-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1234.py
More file actions
23 lines (23 loc) · 701 Bytes
/
1234.py
File metadata and controls
23 lines (23 loc) · 701 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from collections import Counter
class Solution:
def balancedString(self, s: str) -> int:
l, each = len(s), len(s) // 4
start, end = 0, l - 1
c = Counter()
while start < l:
if c[s[start]] == each:
break
c[s[start]] += 1
start += 1
ans = l - start
start -= 1
while end >= start >= -1:
while end >= start >= -1 and c[s[end]] == each:
if start == -1:
return ans
c[s[start]] -= 1
start -= 1
c[s[end]] += 1
end -= 1
ans = min(ans, end - start)
return ans