Skip to content

Commit 2ce2584

Browse files
Merge pull request #2871 from Dhyan-P-Shetty/main
Create: minimum-number-of-flips-to-make-the-binary-string-alternating.py
2 parents afacfad + c667a39 commit 2ce2584

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def minFlips(self, s: str) -> int:
3+
n = len(s)
4+
s = s + s
5+
alt1, alt2 = "", ""
6+
7+
for i in range(len(s)):
8+
alt1 += "0" if i % 2 == 0 else "1"
9+
alt2 += "1" if i % 2 == 0 else "0"
10+
11+
res = float('inf')
12+
diff1, diff2 = 0, 0
13+
l = 0
14+
for r in range(len(s)):
15+
if s[r] != alt1[r]:
16+
diff1 += 1
17+
if s[r] != alt2[r]:
18+
diff2 += 1
19+
if (r - l + 1) > n:
20+
if s[l] != alt1[l]:
21+
diff1 -= 1
22+
if s[l] != alt2[l]:
23+
diff2 -= 1
24+
l += 1
25+
if (r - l + 1) == n:
26+
res = min(res, diff1, diff2)
27+
return res

0 commit comments

Comments
 (0)