File tree 1 file changed +8
-8
lines changed
scripts/algorithms/M/Minimum Changes To Make Alternating Binary String
1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 4 ms (Top 86.00%) | Memory: 41.7 MB (Top 99.33%)
1
2
class Solution {
2
3
public int minOperations (String s ) {
3
4
int count0 = 0 ; // changes required when the string starts from 0
4
5
int count1 = 0 ; // changes required when the string starts from 1
5
-
6
+
6
7
for (int i = 0 ; i < s .length (); i ++){
7
-
8
- // string starts with 1 => all chars at even places should be 1 and that at odd places should be 0
8
+
9
+ // string starts with 1 => all chars at even places should be 1 and that at odd places should be 0
9
10
if ((i % 2 == 0 && s .charAt (i ) == '0' ) || (i % 2 != 0 && s .charAt (i ) == '1' ))
10
11
count1 ++;
11
-
12
- // string starts with 0 => all chars at even places should be 0 and that at odd places should be 1
12
+
13
+ // string starts with 0 => all chars at even places should be 0 and that at odd places should be 1
13
14
else if ((i % 2 == 0 && s .charAt (i ) == '1' ) || (i % 2 != 0 && s .charAt (i ) == '0' ))
14
15
count0 ++;
15
16
}
16
-
17
- // return minimum of the two
17
+
18
+ // return minimum of the two
18
19
return Math .min (count0 , count1 );
19
20
}
20
21
}
21
-
You can’t perform that action at this time.
0 commit comments