Smallest Substring With Identical Characters I

You are given a binary string s of length n and an integer numOps.

You are allowed to perform the following operation on s at most numOps times:

  • Select any index i where 0 <= i < n and flip s[i]. If s[i] == '1', change s[i] to '0', and vice versa.

You need to minimize the length of the longest substring of s such that all the characters in the substring are identical.

Return the minimum length after the operations.

Example 1
Inputs = "000001", num_ops = 1
Output2
Changing s[2] to '1' makes s become "001001", whose longest substrings with identical characters have length 2.
Example 2
Inputs = "0000", num_ops = 2
Output1
Changing s[0] and s[2] to '1' makes s become "1010", so every identical-character substring has length 1.

Constraints

  • 1 <= n == s.length <= 1000
  • s consists only of '0' and '1'.
  • 0 <= numOps <= n

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate