K Smallest Substring
There is a string input_str consisting of characters '0' and '1' only and an integer k. Find a substring of string input_str such that:
kNote: It is guaranteed that answer always exists.
Complete the function getSubstring in the editor below.
getSubstring has the following parameters:
- string
input_str: a string that consists of '0' and '1' - int
k: the number of '1's in the answer
Returns
string: the substring that meets the given conditions
1Example 1
Some of the possible substrings following the first condition:
- "01011"
- "1101"
- "1011"
The substring that is smallest in length and lexicographically smallest is "1011".
It can be proven that there is no other substring that is smaller than "1011" in length and lexicographic order. Hence the answer is "1011".
Constraints
Limits and guarantees your solution can rely on.
input_str is always greater than or equal to k.