Skip to content

2914. Minimum Number of Changes to Make Binary String Beautiful 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  int minChanges(string s) {
    int ans = 0;

    for (int i = 0; i + 1 < s.length(); i += 2)
      if (s[i] != s[i + 1])
        ++ans;

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int minChanges(String s) {
    int ans = 0;

    for (int i = 0; i + 1 < s.length(); i += 2)
      if (s.charAt(i) != s.charAt(i + 1))
        ++ans;

    return ans;
  }
}
1
2
3
class Solution:
  def minChanges(self, s: str) -> int:
    return sum(a != b for a, b in zip(s[::2], s[1::2]))