Problem · String
Get Substring
Learn this problemProblem statement
There is a string input_str consisting of characters '0' and '1' only and an integer k. Find a substring of input_str such that:
- The number of '1's is equal to
k - It has the smallest length
- It is lexicographically smallest
Note: It is guaranteed that answer always exists.
Function
getSubstring(input_str: String, k: int) → String
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
Examples
Example 1
input_str = "0101101"k = 3return = "1011"Some of the possible substrings following the first condition:
- "01011"
- "1101"
- "1011"
Constraints
k ≤ length of input_str ≤ 10^3input_str[i] is in the set {'0', '1'}input_str is always greater than or equal to k.More Wells Fargo problems
- Count Server ReplacementsOA · Seen Feb 2026
- Minimum Remaining LengthOA · Seen Feb 2026
- Compressing ArraySeen Dec 2024
- Sum of Compressed Number for All SubarraysSeen May 2024
- Allocate Wells for Fair DistributionSeen Oct 2023
- Count OperationsSeen Aug 2023
- Find Maximum DistanceSeen Aug 2023
- Find Last Affected SystemSeen May 2022