File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments