Skip to content

Commit a6966ef

Browse files
authored
Create replace-the-substring-for-balanced-string.py
1 parent 09039ae commit a6966ef

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def balancedString(self, s):
9+
"""
10+
:type s: str
11+
:rtype: int
12+
"""
13+
count = collections.Counter(s)
14+
result = len(s)
15+
left = 0
16+
for right in xrange(len(s)):
17+
count[s[right]] -= 1
18+
while left < len(s) and \
19+
all(v <= len(s)//4 for v in count.itervalues()):
20+
result = min(result, right-left+1)
21+
count[s[left]] += 1
22+
left += 1
23+
return result

0 commit comments

Comments
 (0)