Minimum Suffix Flips LeetCode solution in Java

1
class Solution {
2
public int minFlips(String target) {
3

4
boolean lastBit = false;
5

6
int flips = 0;
7
for (int i = 0; i < target.length(); i++) {
8

9
if (target.charAt(i) == '1' && !lastBit) {
10
flips++;
11
lastBit = !lastBit;
12
} else if (target.charAt(i) == '0' && lastBit) {
13
flips++;
14
lastBit = !lastBit;
15
}
16
}
17

18
return flips;
19
}
20
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0