Skip to content

Commit 1a8fc1c

Browse files
Create ReplaceTheSubstringForBalancedString.py
1 parent 0db476d commit 1a8fc1c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Definition for a binary tree node.
2+
import collections
3+
import heapq
4+
import unittest
5+
6+
# Read about enumerate in python
7+
from collections import defaultdict
8+
from typing import List
9+
10+
class ReplaceTheSubstringForBalancedString(unittest.TestCase):
11+
12+
def balancedString(self, s: str) -> int:
13+
14+
histogram = collections.Counter(s)
15+
result = n = len(s)
16+
left = 0
17+
for right, rightValue in enumerate(s):
18+
histogram[rightValue] -= 1
19+
while left < n and all(n / 4 >= histogram[value] for value in "QWER"):
20+
result = min(result, right - left + 1)
21+
histogram[s[left]] += 1
22+
left += 1
23+
24+
return result
25+
26+
def test_Leetcode(self):
27+
self.assertEqual(0, self.balancedString("QWER"))
28+
self.assertEqual(1, self.balancedString("QQWE"))
29+
self.assertEqual(2, self.balancedString("QQQW"))
30+
self.assertEqual(3, self.balancedString("QQQQ"))
31+
32+
if __name__ == '__main__':
33+
unittest.main()

0 commit comments

Comments
 (0)