Skip to content

3399. Smallest Substring With Identical Characters II 👍

  • Time: $O(n\log n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
 public:
  // Same as 3398. Smallest Substring With Identical Characters I
  int minLength(string s, int numOps) {
    int l = 1;
    int r = s.length();

    while (l < r) {
      const int m = (l + r) / 2;
      if (getMinOps(s, m) <= numOps)
        r = m;
      else
        l = m + 1;
    }

    return l;
  }

 private:
  // Returns the minimum number of operations needed to make all groups of
  // identical characters of length k or less.
  int getMinOps(const string& s, int k) {
    if (k == 1) {
      size_t res = 0;
      for (int i = 0; i < s.length(); ++i)
        if (s[i] - '0' == i % 2)
          ++res;
      return min(res, s.length() - res);
    }

    int res = 0;
    int runningLen = 1;

    for (int i = 1; i < s.length(); ++i)
      if (s[i] == s[i - 1]) {
        ++runningLen;
      } else {
        res += runningLen / (k + 1);
        runningLen = 1;
      }

    return res + runningLen / (k + 1);
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
  // Same as 3398. Smallest Substring With Identical Characters I
  public int minLength(String s, int numOps) {
    int l = 1;
    int r = s.length();

    while (l < r) {
      final int m = (l + r) / 2;
      if (getMinOps(s, m) <= numOps)
        r = m;
      else
        l = m + 1;
    }

    return l;
  }

  // Returns the minimum number of operations needed to make all groups of
  // identical characters of length k or less.
  private int getMinOps(final String s, int k) {
    if (k == 1) {
      int res = 0;
      for (int i = 0; i < s.length(); ++i)
        if (s.charAt(i) - '0' == i % 2)
          ++res;
      return Math.min(res, s.length() - res);
    }

    int res = 0;
    int runningLen = 1;

    for (int i = 1; i < s.length(); ++i)
      if (s.charAt(i) == s.charAt(i - 1)) {
        ++runningLen;
      } else {
        res += runningLen / (k + 1);
        runningLen = 1;
      }

    return res + runningLen / (k + 1);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
  # Same as 3398. Smallest Substring With Identical Characters I
  def minLength(self, s: str, numOps: int) -> int:
    def getMinOps(k: int) -> int:
      """
      Returns the minimum number of operations needed to make all groups of
      identical characters of length k or less.
      """
      if k == 1:
        res = sum(1 for i, c in enumerate(s) if int(c) == i % 2)
        return min(res, len(s) - res)

      res = 0
      runningLen = 1

      for a, b in itertools.pairwise(s):
        if a == b:
          runningLen += 1
        else:
          res += runningLen // (k + 1)
          runningLen = 1

      return res + runningLen // (k + 1)

    return bisect_left(range(1, len(s) + 1),
                       True, key=lambda m: getMinOps(m) <= numOps)