Skip to content

2027. Minimum Moves to Convert String 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  int minimumMoves(string s) {
    int ans = 0;

    for (int i = 0; i < s.length();)
      if (s[i] == 'O') {
        ++i;
      } else {
        ++ans;
        i += 3;
      }

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

    for (int i = 0; i < s.length();)
      if (s.charAt(i) == 'O') {
        ++i;
      } else {
        ++ans;
        i += 3;
      }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
  def minimumMoves(self, s: str) -> int:
    ans = 0

    i = 0
    while i < len(s):
      if s[i] == 'O':
        i += 1
      else:
        ans += 1
        i += 3

    return ans