Skip to content

Commit 2fd47c0

Browse files
authored
Improved task 2027.
1 parent 54d39ea commit 2fd47c0

File tree

1 file changed

+9
-14
lines changed
  • src/main/java/g2001_2100/s2027_minimum_moves_to_convert_string

1 file changed

+9
-14
lines changed
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package g2001_2100.s2027_minimum_moves_to_convert_string;
22

3-
// #Easy #String #Greedy #2022_05_25_Time_4_ms_(5.74%)_Space_42.1_MB_(37.50%)
3+
// #Easy #String #Greedy #2022_05_25_Time_0_ms_(100.00%)_Space_40_MB_(97.32%)
44

55
public class Solution {
66
public int minimumMoves(String s) {
7-
int moves = 0;
7+
int r = 0;
88
int i = 0;
9-
while (i < s.length()) {
10-
if (s.charAt(i) == 'O') {
11-
i++;
12-
continue;
13-
}
14-
String candidate = i + 3 <= s.length() ? s.substring(i, i + 3) : s.substring(i);
15-
if (candidate.contains("X")) {
16-
moves++;
17-
i += 3;
18-
} else {
19-
i++;
9+
char[] sArray = s.toCharArray();
10+
while (i < sArray.length) {
11+
if (sArray[i] == 'X') {
12+
r++;
13+
i += 2;
2014
}
15+
i++;
2116
}
22-
return moves;
17+
return r;
2318
}
2419
}

0 commit comments

Comments
 (0)