Skip to content

Commit 9d8cfc7

Browse files
chore: add LeetCode daily solution
1 parent e0e1984 commit 9d8cfc7

5 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Minimum Number of Flips to Make the Binary String Alternating (Medium)
2+
3+
**Problem ID:** 1888
4+
**Date:** 2026-03-07
5+
**Link:** https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/
6+
7+
## Approach
8+
9+
To solve the problem of finding the minimum number of flips required to make a binary string alternating, we can utilize a systematic approach that involves analyzing the string's structure and potential transformations.
10+
11+
### Approach:
12+
13+
1. **Understanding Alternating Patterns**:
14+
An alternating binary string can either start with '0' or '1', leading to two possible valid patterns:
15+
- Pattern A: "010101..."
16+
- Pattern B: "101010..."
17+
18+
2. **Counting Flips**:
19+
For each character in the string, we will compare it against the expected character in both patterns:
20+
- For Pattern A, the expected character at index `i` is `i % 2 == 0 ? '0' : '1'`.
21+
- For Pattern B, the expected character at index `i` is `i % 2 == 0 ? '1' : '0'`.
22+
23+
As we traverse the string, we will maintain two counters:
24+
- `flipsA`: Counts the number of flips needed to convert the string to Pattern A.
25+
- `flipsB`: Counts the number of flips needed to convert the string to Pattern B.
26+
27+
3. **Handling Rotations**:
28+
Since we can perform Type-1 operations (rotating the string), we need to consider all possible rotations of the string. However, instead of generating each rotation, we can simulate this by considering the string as circular. This can be efficiently done by concatenating the string to itself (i.e., `s + s`), allowing us to check for alternating patterns starting from any index in the original string.
29+
30+
4. **Sliding Window Technique**:
31+
We can use a sliding window of size equal to the original string length to evaluate the number of flips required for each segment of length `n` in the concatenated string. For each window position, we compute the flips required for both patterns and update the minimum flips accordingly.
32+
33+
5. **Final Calculation**:
34+
The result will be the minimum value between `flipsA` and `flipsB` across all evaluated windows.
35+
36+
### Data Structures:
37+
- We primarily use integer counters for `flipsA` and `flipsB`.
38+
- A single string variable for the binary string and its concatenated version.
39+
40+
### Complexity:
41+
- **Time Complexity**: O(n), where n is the length of the string. We perform a single pass over the string and a sliding window check, both of which operate in linear time.
42+
- **Space Complexity**: O(1) for the counters, as we do not use any additional data structures that scale with input size.
43+
44+
This approach ensures that we efficiently compute the minimum flips required to make the binary string alternating while considering all possible rotations without explicitly generating them.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int minFlips(String s) {
3+
int n = s.length();
4+
s += s; // Concatenate the string to handle rotations
5+
int count1 = 0, count2 = 0;
6+
7+
// Count flips for both patterns: "010101..." and "101010..."
8+
for (int i = 0; i < n; i++) {
9+
if (s.charAt(i) != '0' + (i % 2)) count1++;
10+
if (s.charAt(i) != '1' - (i % 2)) count2++;
11+
}
12+
13+
int minFlips = Math.min(count1, count2);
14+
for (int i = n; i < s.length(); i++) {
15+
// Slide the window
16+
if (s.charAt(i) != '0' + (i % 2)) count1++;
17+
if (s.charAt(i) != '1' - (i % 2)) count2++;
18+
if (s.charAt(i - n) != '0' + ((i - n) % 2)) count1--;
19+
if (s.charAt(i - n) != '1' - ((i - n) % 2)) count2--;
20+
minFlips = Math.min(minFlips, Math.min(count1, count2));
21+
}
22+
23+
return minFlips;
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var minFlips = function(s) {
2+
const n = s.length;
3+
s += s; // Concatenate the string to handle rotations
4+
let count1 = 0, count2 = 0;
5+
6+
for (let i = 0; i < n; i++) {
7+
// Expected characters for alternating patterns
8+
if (s[i] !== (i % 2 === 0 ? '0' : '1')) count1++;
9+
if (s[i] !== (i % 2 === 0 ? '1' : '0')) count2++;
10+
}
11+
12+
let minFlips = Math.min(count1, count2);
13+
14+
for (let i = n; i < s.length; i++) {
15+
// Slide the window
16+
if (s[i - n] !== (i - n % 2 === 0 ? '0' : '1')) count1--;
17+
if (s[i - n] !== (i - n % 2 === 0 ? '1' : '0')) count2--;
18+
19+
if (s[i] !== (i % 2 === 0 ? '0' : '1')) count1++;
20+
if (s[i] !== (i % 2 === 0 ? '1' : '0')) count2++;
21+
22+
minFlips = Math.min(minFlips, count1, count2);
23+
}
24+
25+
return minFlips;
26+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def minFlips(self, s: str) -> int:
3+
n = len(s)
4+
s = s + s # Concatenate the string to handle rotations
5+
count1 = count2 = 0
6+
7+
# Count flips for the two possible alternating patterns
8+
for i in range(n):
9+
if s[i] != '0' if i % 2 == 0 else '1':
10+
count1 += 1
11+
if s[i] != '1' if i % 2 == 0 else '0':
12+
count2 += 1
13+
14+
# Minimum flips needed for any rotation
15+
min_flips = min(count1, count2)
16+
17+
for i in range(n, len(s)):
18+
if s[i] != '0' if i % 2 == 0 else '1':
19+
count1 += 1
20+
if s[i] != '1' if i % 2 == 0 else '0':
21+
count2 += 1
22+
23+
if s[i - n] != '0' if (i - n) % 2 == 0 else '1':
24+
count1 -= 1
25+
if s[i - n] != '1' if (i - n) % 2 == 0 else '0':
26+
count2 -= 1
27+
28+
min_flips = min(min_flips, count1, count2)
29+
30+
return min_flips

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
385385
- 2026-03-04 — [Special Positions in a Binary Matrix](https://leetcode.com/problems/special-positions-in-a-binary-matrix/) (Easy) → `Easy/2026-03-04-1582-Special-Positions-in-a-Binary-Matrix`
386386
- 2026-03-05 — [Minimum Changes To Make Alternating Binary String](https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/) (Easy) → `Easy/2026-03-05-1758-Minimum-Changes-To-Make-Alternating-Binary-String`
387387
- 2026-03-06 — [Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/) (Easy) → `Easy/2026-03-06-1784-Check-if-Binary-String-Has-at-Most-One-Segment-of-Ones`
388+
- 2026-03-07 — [Minimum Number of Flips to Make the Binary String Alternating](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/) (Medium) → `Medium/2026-03-07-1888-Minimum-Number-of-Flips-to-Make-the-Binary-String-Alternating`

0 commit comments

Comments
 (0)