Skip to content

Commit 6430064

Browse files
chore: add LeetCode daily solution
1 parent 1f0edc1 commit 6430064

5 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Minimum Changes To Make Alternating Binary String (Easy)
2+
3+
**Problem ID:** 1758
4+
**Date:** 2026-03-05
5+
**Link:** https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/
6+
7+
## Approach
8+
9+
To solve the problem of converting a binary string into an alternating format with the minimum number of changes, we can follow a systematic approach:
10+
11+
### Approach:
12+
13+
1. **Understanding Alternating Patterns**:
14+
An alternating binary string can start with either '0' or '1'. Therefore, there are two possible valid patterns for a string of length `n`:
15+
- Pattern A: "010101..." (starts with '0')
16+
- Pattern B: "101010..." (starts with '1')
17+
18+
2. **Count Mismatches**:
19+
We will iterate through the given string `s` and compare each character to the expected character in both patterns (A and B). For each index `i`:
20+
- If `s[i]` does not match the expected character in Pattern A, we increment a mismatch counter for Pattern A.
21+
- Similarly, if `s[i]` does not match the expected character in Pattern B, we increment a mismatch counter for Pattern B.
22+
23+
3. **Calculate Minimum Changes**:
24+
After completing the iteration, we will have two counts:
25+
- `changesA`: Number of changes needed to convert `s` to Pattern A.
26+
- `changesB`: Number of changes needed to convert `s` to Pattern B.
27+
The result will be the minimum of these two counts, i.e., `min(changesA, changesB)`.
28+
29+
### Data Structures:
30+
- We primarily use integer counters to keep track of mismatches for both patterns, which requires O(1) space. The iteration through the string requires O(n) time.
31+
32+
### Complexity:
33+
- **Time Complexity**: O(n), where `n` is the length of the string `s`. We traverse the string once to count mismatches.
34+
- **Space Complexity**: O(1), as we only use a fixed number of extra variables for counting.
35+
36+
This approach efficiently determines the minimum number of changes required to transform the input string into an alternating binary string by leveraging direct comparison to the expected patterns.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int minOperations(String s) {
3+
int count1 = 0, count2 = 0;
4+
for (int i = 0; i < s.length(); i++) {
5+
char expectedChar = (i % 2 == 0) ? '0' : '1';
6+
if (s.charAt(i) != expectedChar) {
7+
count1++;
8+
}
9+
expectedChar = (i % 2 == 0) ? '1' : '0';
10+
if (s.charAt(i) != expectedChar) {
11+
count2++;
12+
}
13+
}
14+
return Math.min(count1, count2);
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var minOperations = function(s) {
2+
let count1 = 0, count2 = 0;
3+
4+
for (let i = 0; i < s.length; i++) {
5+
const expectedChar1 = i % 2 === 0 ? '0' : '1';
6+
const expectedChar2 = i % 2 === 0 ? '1' : '0';
7+
8+
if (s[i] !== expectedChar1) count1++;
9+
if (s[i] !== expectedChar2) count2++;
10+
}
11+
12+
return Math.min(count1, count2);
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minOperations(self, s: str) -> int:
3+
count1 = count2 = 0
4+
for i in range(len(s)):
5+
expected_char1 = '0' if i % 2 == 0 else '1'
6+
expected_char2 = '1' if i % 2 == 0 else '0'
7+
if s[i] != expected_char1:
8+
count1 += 1
9+
if s[i] != expected_char2:
10+
count2 += 1
11+
return min(count1, count2)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
383383
- 2026-03-02 — [Minimum Swaps to Arrange a Binary Grid](https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/) (Medium) → `Medium/2026-03-02-1536-Minimum-Swaps-to-Arrange-a-Binary-Grid`
384384
- 2026-03-03 — [Find Kth Bit in Nth Binary String](https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/) (Medium) → `Medium/2026-03-03-1545-Find-Kth-Bit-in-Nth-Binary-String`
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`
386+
- 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`

0 commit comments

Comments
 (0)