Skip to content

Commit c0ca05c

Browse files
committed
Create 1758-MinimumChangesToMakeAlternatingBinaryString.py
1 parent fa51ce4 commit c0ca05c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
"""
3+
CREATED AT: 2022-11-29
4+
5+
URL: https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/
6+
7+
GITHUB: https://github.com/Jiezhi/myleetcode
8+
9+
FileName: 1758-MinimumChangesToMakeAlternatingBinaryString
10+
11+
Difficulty: Easy
12+
13+
Desc:
14+
15+
Tag:
16+
17+
See:
18+
19+
"""
20+
21+
22+
class Solution:
23+
def minOperations(self, s: str) -> int:
24+
"""
25+
1 <= s.length <= 10^4
26+
s[i] is either '0' or '1'.
27+
"""
28+
# ret1 for start with 0
29+
# ret2 for start with 1
30+
ret1, ret2 = 0, 0
31+
for i, c in enumerate(s):
32+
if i & 1:
33+
if c == '1':
34+
ret2 += 1
35+
else:
36+
ret1 += 1
37+
elif c == '1':
38+
ret1 += 1
39+
else:
40+
ret2 += 1
41+
return min(ret1, ret2)
42+
43+
44+
def test():
45+
assert Solution().minOperations(s="0100") == 1
46+
assert Solution().minOperations(s="10") == 0
47+
assert Solution().minOperations(s="1111") == 2
48+
49+
50+
if __name__ == '__main__':
51+
test()

0 commit comments

Comments
 (0)