forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2380.java
31 lines (29 loc) · 975 Bytes
/
_2380.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.fishercoder.solutions;
public class _2380 {
public static class Solution1 {
public int secondsToRemoveOccurrences(String s) {
int seconds = 0;
StringBuilder sb = new StringBuilder(s);
while (hasZeroOneCount(sb)) {
for (int i = 0; i < sb.length() - 1; ) {
if (sb.charAt(i) == '0' && sb.charAt(i + 1) == '1') {
sb.setCharAt(i++, '1');
sb.setCharAt(i++, '0');
} else {
i++;
}
}
seconds++;
}
return seconds;
}
private boolean hasZeroOneCount(StringBuilder sb) {
for (int i = 0; i < sb.length() - 1; i++) {
if (sb.charAt(i) == '0' && sb.charAt(i + 1) == '1') {
return true;
}
}
return false;
}
}
}