Skip to content

1529. Minimum Suffix Flips 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  int minFlips(string target) {
    int ans = 0;
    int state = 0;

    for (const char c : target)
      if (c - '0' != state) {
        state = c - '0';
        ++ans;
      }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
  public int minFlips(String target) {
    int ans = 0;
    int state = 0;

    for (final char c : target.toCharArray())
      if (c - '0' != state) {
        state = c - '0';
        ++ans;
      }

    return ans;
  }
}