-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMinimumChangesToMakeAlternatingBinaryString.java
54 lines (53 loc) · 1.6 KB
/
MinimumChangesToMakeAlternatingBinaryString.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package io.ziheng.bitmanipulation.leetcode;
/**
* LeetCode 1758. Minimum Changes To Make Alternating Binary String
* https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/
*/
public class MinimumChangesToMakeAlternatingBinaryString {
public static void main(String[] args) {
MinimumChangesToMakeAlternatingBinaryString obj = new MinimumChangesToMakeAlternatingBinaryString();
String s = "0100";
System.out.println(s + " -> " + obj.minOperations(s));
s = "1111";
System.out.println(s + " -> " + obj.minOperations(s));
s = "10";
System.out.println(s + " -> " + obj.minOperations(s));
}
public int minOperations(String s) {
if (s == null || s.length() == 0) {
return 0;
}
char[] sArr = s.toCharArray();
char patternA = '1';
char patternB = '0';
int cntA = 0;
int cntB = 0;
for (int i = 0; i < sArr.length; i++) {
if (sArr[i] != patternA) {
cntA++;
}
patternA = flipBit(patternA);
}
for (int i = 0; i < sArr.length; i++) {
if (sArr[i] != patternB) {
cntB++;
}
patternB = flipBit(patternB);
}
return minInt(cntA, cntB);
}
private char flipBit(char b) {
switch (b) {
case '0':
return '1';
case '1':
return '0';
default:
return '0';
}
}
private int minInt(int a, int b) {
return a < b ? a : b;
}
}
/* EOF */