-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_28.java
More file actions
31 lines (26 loc) · 951 Bytes
/
Problem_28.java
File metadata and controls
31 lines (26 loc) · 951 Bytes
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 strings;
// Number of flips to make binary string alternate
public class Problem_28 {
public static int minFlips(String A) {
int flipTo0 = 0, flipTo1 = 0;
// Iterate through the string
for (int i = 0; i < A.length(); i++) {
char expected = (i % 2 == 0) ? '0' : '1'; // Expected character based on even/odd position
// Count flips if the current character is different from expected
if (A.charAt(i) != expected) {
if (expected == '0') {
flipTo0++;
} else {
flipTo1++;
}
}
}
// Return the minimum number of flips
return Math.min(flipTo0, flipTo1);
}
public static void main(String[] args) {
String A = "001";
int minFlips = minFlips(A);
System.out.println("Minimum number of flips for " + A + ": " + minFlips);
}
}