Skip to content

Commit 187f3cc

Browse files
authored
Merge pull request doocs#236 from phuclhv/master
Add Solution.py to problem 1234.Replace the Substring for Balanced String
2 parents 8ef96d4 + 1091fee commit 187f3cc

File tree

1 file changed

+91
-0
lines changed
  • solution/1234.Replace the Substring for Balanced String

1 file changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'''
2+
https://leetcode.com/problems/replace-the-substring-for-balanced-string/
3+
4+
You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'.
5+
6+
A string is said to be balanced if each of its characters appears n/4 times where n is the length of the string.
7+
8+
Return the minimum length of the substring that can be replaced with any other string of the same length to make the original string s balanced.
9+
10+
Return 0 if the string is already balanced.
11+
12+
13+
14+
Example 1:
15+
16+
Input: s = "QWER"
17+
Output: 0
18+
Explanation: s is already balanced.
19+
Example 2:
20+
21+
Input: s = "QQWE"
22+
Output: 1
23+
Explanation: We need to replace a 'Q' to 'R', so that "RQWE" (or "QRWE") is balanced.
24+
Example 3:
25+
26+
Input: s = "QQQW"
27+
Output: 2
28+
Explanation: We can replace the first "QQ" to "ER".
29+
Example 4:
30+
31+
Input: s = "QQQQ"
32+
Output: 3
33+
Explanation: We can replace the last 3 'Q' to make s = "QWER".
34+
35+
36+
Constraints:
37+
38+
1 <= s.length <= 10^5
39+
s.length is a multiple of 4
40+
s contains only 'Q', 'W', 'E' and 'R'.
41+
'''
42+
'''
43+
Runtime: 172 ms, faster than 92.84% of Python3 online submissions for Replace the Substring for Balanced String.
44+
Memory Usage: 13.4 MB, less than 100.00% of Python3 online submissions for Replace the Substring for Balanced String.
45+
46+
'''
47+
48+
49+
from collections import defaultdict, Counter
50+
class Solution:
51+
def balancedString(self, s: str) -> int:
52+
# count the occurence of each char
53+
count_chars = Counter(s)
54+
55+
required = len(s) // 4
56+
57+
# hold the number of excessive occurences
58+
more_chars = defaultdict(int)
59+
for char, count_char in count_chars.items():
60+
more_chars[char] = max(0, count_char - required)
61+
62+
min_len = len(s)
63+
64+
# count the number of total replacements
65+
need_replace = sum(more_chars.values())
66+
if need_replace == 0:
67+
return 0
68+
69+
# Sliding windows
70+
# First, move the second cursors until satisfy the conditions
71+
# Second, move the first_cursor so that it still satisfy the requirement
72+
73+
first_cursor, second_cursor = 0, 0
74+
while second_cursor < len(s):
75+
# Move second_cursor
76+
if more_chars[s[second_cursor]] > 0:
77+
need_replace -= 1
78+
more_chars[s[second_cursor]] -= 1
79+
second_cursor += 1
80+
81+
# Move first_cursor
82+
while first_cursor < second_cursor and need_replace == 0:
83+
min_len = min(min_len, second_cursor - first_cursor)
84+
if s[first_cursor] in more_chars:
85+
more_chars[s[first_cursor]] += 1
86+
if more_chars[s[first_cursor]] > 0:
87+
need_replace += 1
88+
first_cursor += 1
89+
90+
return min_len
91+

0 commit comments

Comments
 (0)