-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
33 lines (28 loc) · 818 Bytes
/
solution.py
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
class Solution:
def minFlips(self, s: str) -> int:
n = len(s)
s = s + s
alt1, alt2 = "", ""
for i in range(len(s)):
alt1 += "0" if i % 2 else "1"
alt2 += "1" if i % 2 else "0"
res = len(s)
diff1, diff2 = 0, 0
l = 0
for r in range(len(s)):
if s[r] != alt1[r]:
diff1 += 1
if s[r] != alt2[r]:
diff2 += 1
if (r - l + 1) > n:
if s[l] != alt1[l]:
diff1 -= 1
if s[l] != alt2[l]:
diff2 -= 1
l += 1
if (r - l + 1) == n:
res = min(res, diff1, diff2)
return res
if __name__ == '__main__':
s = "111000"
print(Solution().minFlips(s))