1
# Runtime: 450 ms (Top 49.65%) | Memory: 14.6 MB (Top 82.17%)
2
class Solution:
3
def balancedString(self, s):
4
count = collections.Counter(s)
5
res = n = len(s)
6
if all(n / 4 == count[char] for char in "QWER"):
7
return 0
8
left = 0
9
for right, char in enumerate(s):
10
# replace char whose index==right to check if it is balanced
11
count[char] -= 1
12
# if it is balanced, window shrinks to get the smallest length of window
13
while left <= right and all(n / 4 >= count[char] for char in "QWER"):
14
res = min(res, right - left + 1)
15
count[s[left]] = count[s[left]] + 1
16
left += 1
17
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0