Skip to content

Commit d61d5c5

Browse files
committed
feat: add solutions to lc problem: No.1234
No.1234.Replace the Substring for Balanced String
1 parent 4cd0c93 commit d61d5c5

File tree

7 files changed

+340
-114
lines changed

7 files changed

+340
-114
lines changed

solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,55 @@ func removeSubfolders(folder []string) []string {
347347
}
348348
```
349349

350+
```go
351+
type Trie struct {
352+
children map[string]*Trie
353+
fid int
354+
}
355+
356+
func newTrie() *Trie {
357+
return &Trie{map[string]*Trie{}, -1}
358+
}
359+
360+
func (this *Trie) insert(fid int, f string) {
361+
node := this
362+
ps := strings.Split(f, "/")
363+
for _, p := range ps[1:] {
364+
if _, ok := node.children[p]; !ok {
365+
node.children[p] = newTrie()
366+
}
367+
node = node.children[p]
368+
}
369+
node.fid = fid
370+
}
371+
372+
func (this *Trie) search() (ans []int) {
373+
var dfs func(*Trie)
374+
dfs = func(root *Trie) {
375+
if root.fid != -1 {
376+
ans = append(ans, root.fid)
377+
return
378+
}
379+
for _, child := range root.children {
380+
dfs(child)
381+
}
382+
}
383+
dfs(this)
384+
return
385+
}
386+
387+
func removeSubfolders(folder []string) (ans []string) {
388+
trie := newTrie()
389+
for i, f := range folder {
390+
trie.insert(i, f)
391+
}
392+
for _, i := range trie.search() {
393+
ans = append(ans, folder[i])
394+
}
395+
return
396+
}
397+
```
398+
350399
### **...**
351400

352401
```

solution/1200-1299/1234.Replace the Substring for Balanced String/README.md

+108-38
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:计数 + 双指针**
71+
72+
我们先用一个哈希表或数组 `cnt` 统计字符串 $s$ 中每个字符的数量,如果所有字符的数量都不超过 $n/4$,那么字符串 $s$ 就是平衡字符串,直接返回 $0$。
73+
74+
否则,我们使用双指针 $j$ 和 $i$ 分别维护窗口的左右边界,初始时 $j = 0$。
75+
76+
接下来,从左到右遍历字符串 $s$,每次遍历到一个字符,就将该字符的数量减 $1$,然后判断当前窗口是否满足条件,即窗口外的字符数量都不超过 $n/4$。如果满足条件,那么就更新答案,然后将窗口的左边界右移,直到不满足条件为止。
77+
78+
最后,返回答案即可。
79+
80+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度;而 $C$ 是字符集的大小,本题中 $C = 4$。
81+
7082
<!-- tabs:start -->
7183

7284
### **Python3**
@@ -76,53 +88,111 @@
7688
```python
7789
class Solution:
7890
def balancedString(self, s: str) -> int:
79-
# count the occurence of each char
80-
count_chars = Counter(s)
81-
82-
required = len(s) // 4
83-
84-
# hold the number of excessive occurences
85-
more_chars = defaultdict(int)
86-
for char, count_char in count_chars.items():
87-
more_chars[char] = max(0, count_char - required)
88-
89-
min_len = len(s)
90-
91-
# count the number of total replacements
92-
need_replace = sum(more_chars.values())
93-
if need_replace == 0:
91+
cnt = Counter(s)
92+
n = len(s)
93+
if all(v <= n // 4 for v in cnt.values()):
9494
return 0
95-
96-
# Sliding windows
97-
# First, move the second cursors until satisfy the conditions
98-
# Second, move the first_cursor so that it still satisfy the requirement
99-
100-
first_cursor, second_cursor = 0, 0
101-
while second_cursor < len(s):
102-
# Move second_cursor
103-
if more_chars[s[second_cursor]] > 0:
104-
need_replace -= 1
105-
more_chars[s[second_cursor]] -= 1
106-
second_cursor += 1
107-
108-
# Move first_cursor
109-
while first_cursor < second_cursor and need_replace == 0:
110-
min_len = min(min_len, second_cursor - first_cursor)
111-
if s[first_cursor] in more_chars:
112-
more_chars[s[first_cursor]] += 1
113-
if more_chars[s[first_cursor]] > 0:
114-
need_replace += 1
115-
first_cursor += 1
116-
117-
return min_len
95+
ans, j = n, 0
96+
for i, c in enumerate(s):
97+
cnt[c] -= 1
98+
while j <= i and all(v <= n // 4 for v in cnt.values()):
99+
ans = min(ans, i - j + 1)
100+
cnt[s[j]] += 1
101+
j += 1
102+
return ans
118103
```
119104

120105
### **Java**
121106

122107
<!-- 这里可写当前语言的特殊实现逻辑 -->
123108

124109
```java
110+
class Solution {
111+
public int balancedString(String s) {
112+
int[] cnt = new int[4];
113+
String t = "QWER";
114+
int n = s.length();
115+
for (int i = 0; i < n; ++i) {
116+
cnt[t.indexOf(s.charAt(i))]++;
117+
}
118+
int m = n / 4;
119+
if (cnt[0] == m && cnt[1] == m && cnt[2] == m && cnt[3] == m) {
120+
return 0;
121+
}
122+
int ans = n;
123+
for (int i = 0, j = 0; i < n; ++i) {
124+
cnt[t.indexOf(s.charAt(i))]--;
125+
while (j <= i && cnt[0] <= m && cnt[1] <= m && cnt[2] <= m && cnt[3] <= m) {
126+
ans = Math.min(ans, i - j + 1);
127+
cnt[t.indexOf(s.charAt(j++))]++;
128+
}
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
int balancedString(string s) {
141+
int cnt[4]{};
142+
string t = "QWER";
143+
int n = s.size();
144+
for (char& c : s) {
145+
cnt[t.find(c)]++;
146+
}
147+
int m = n / 4;
148+
if (cnt[0] == m && cnt[1] == m && cnt[2] == m && cnt[3] == m) {
149+
return 0;
150+
}
151+
int ans = n;
152+
for (int i = 0, j = 0; i < n; ++i) {
153+
cnt[t.find(s[i])]--;
154+
while (j <= i && cnt[0] <= m && cnt[1] <= m && cnt[2] <= m && cnt[3] <= m) {
155+
ans = min(ans, i - j + 1);
156+
cnt[t.find(s[j++])]++;
157+
}
158+
}
159+
return ans;
160+
}
161+
};
162+
```
125163
164+
### **Go**
165+
166+
```go
167+
func balancedString(s string) int {
168+
cnt := [4]int{}
169+
t := "QWER"
170+
n := len(s)
171+
for i := range s {
172+
cnt[strings.IndexByte(t, s[i])]++
173+
}
174+
m := n / 4
175+
if cnt[0] == m && cnt[1] == m && cnt[2] == m && cnt[3] == m {
176+
return 0
177+
}
178+
ans := n
179+
for i, j := 0, 0; i < n; i++ {
180+
cnt[strings.IndexByte(t, s[i])]--
181+
for j <= i && cnt[0] <= m && cnt[1] <= m && cnt[2] <= m && cnt[3] <= m {
182+
ans = min(ans, i-j+1)
183+
cnt[strings.IndexByte(t, s[j])]++
184+
j++
185+
}
186+
}
187+
return ans
188+
}
189+
190+
func min(a, b int) int {
191+
if a < b {
192+
return a
193+
}
194+
return b
195+
}
126196
```
127197

128198
### **...**

solution/1200-1299/1234.Replace the Substring for Balanced String/README_EN.md

+96-38
Original file line numberDiff line numberDiff line change
@@ -54,51 +54,109 @@
5454
```python
5555
class Solution:
5656
def balancedString(self, s: str) -> int:
57-
# count the occurence of each char
58-
count_chars = Counter(s)
59-
60-
required = len(s) // 4
61-
62-
# hold the number of excessive occurences
63-
more_chars = defaultdict(int)
64-
for char, count_char in count_chars.items():
65-
more_chars[char] = max(0, count_char - required)
66-
67-
min_len = len(s)
68-
69-
# count the number of total replacements
70-
need_replace = sum(more_chars.values())
71-
if need_replace == 0:
57+
cnt = Counter(s)
58+
n = len(s)
59+
if all(v <= n // 4 for v in cnt.values()):
7260
return 0
73-
74-
# Sliding windows
75-
# First, move the second cursors until satisfy the conditions
76-
# Second, move the first_cursor so that it still satisfy the requirement
77-
78-
first_cursor, second_cursor = 0, 0
79-
while second_cursor < len(s):
80-
# Move second_cursor
81-
if more_chars[s[second_cursor]] > 0:
82-
need_replace -= 1
83-
more_chars[s[second_cursor]] -= 1
84-
second_cursor += 1
85-
86-
# Move first_cursor
87-
while first_cursor < second_cursor and need_replace == 0:
88-
min_len = min(min_len, second_cursor - first_cursor)
89-
if s[first_cursor] in more_chars:
90-
more_chars[s[first_cursor]] += 1
91-
if more_chars[s[first_cursor]] > 0:
92-
need_replace += 1
93-
first_cursor += 1
94-
95-
return min_len
61+
ans, j = n, 0
62+
for i, c in enumerate(s):
63+
cnt[c] -= 1
64+
while j <= i and all(v <= n // 4 for v in cnt.values()):
65+
ans = min(ans, i - j + 1)
66+
cnt[s[j]] += 1
67+
j += 1
68+
return ans
9669
```
9770

9871
### **Java**
9972

10073
```java
74+
class Solution {
75+
public int balancedString(String s) {
76+
int[] cnt = new int[4];
77+
String t = "QWER";
78+
int n = s.length();
79+
for (int i = 0; i < n; ++i) {
80+
cnt[t.indexOf(s.charAt(i))]++;
81+
}
82+
int m = n / 4;
83+
if (cnt[0] == m && cnt[1] == m && cnt[2] == m && cnt[3] == m) {
84+
return 0;
85+
}
86+
int ans = n;
87+
for (int i = 0, j = 0; i < n; ++i) {
88+
cnt[t.indexOf(s.charAt(i))]--;
89+
while (j <= i && cnt[0] <= m && cnt[1] <= m && cnt[2] <= m && cnt[3] <= m) {
90+
ans = Math.min(ans, i - j + 1);
91+
cnt[t.indexOf(s.charAt(j++))]++;
92+
}
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int balancedString(string s) {
105+
int cnt[4]{};
106+
string t = "QWER";
107+
int n = s.size();
108+
for (char& c : s) {
109+
cnt[t.find(c)]++;
110+
}
111+
int m = n / 4;
112+
if (cnt[0] == m && cnt[1] == m && cnt[2] == m && cnt[3] == m) {
113+
return 0;
114+
}
115+
int ans = n;
116+
for (int i = 0, j = 0; i < n; ++i) {
117+
cnt[t.find(s[i])]--;
118+
while (j <= i && cnt[0] <= m && cnt[1] <= m && cnt[2] <= m && cnt[3] <= m) {
119+
ans = min(ans, i - j + 1);
120+
cnt[t.find(s[j++])]++;
121+
}
122+
}
123+
return ans;
124+
}
125+
};
126+
```
101127
128+
### **Go**
129+
130+
```go
131+
func balancedString(s string) int {
132+
cnt := [4]int{}
133+
t := "QWER"
134+
n := len(s)
135+
for i := range s {
136+
cnt[strings.IndexByte(t, s[i])]++
137+
}
138+
m := n / 4
139+
if cnt[0] == m && cnt[1] == m && cnt[2] == m && cnt[3] == m {
140+
return 0
141+
}
142+
ans := n
143+
for i, j := 0, 0; i < n; i++ {
144+
cnt[strings.IndexByte(t, s[i])]--
145+
for j <= i && cnt[0] <= m && cnt[1] <= m && cnt[2] <= m && cnt[3] <= m {
146+
ans = min(ans, i-j+1)
147+
cnt[strings.IndexByte(t, s[j])]++
148+
j++
149+
}
150+
}
151+
return ans
152+
}
153+
154+
func min(a, b int) int {
155+
if a < b {
156+
return a
157+
}
158+
return b
159+
}
102160
```
103161

104162
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int balancedString(string s) {
4+
int cnt[4]{};
5+
string t = "QWER";
6+
int n = s.size();
7+
for (char& c : s) {
8+
cnt[t.find(c)]++;
9+
}
10+
int m = n / 4;
11+
if (cnt[0] == m && cnt[1] == m && cnt[2] == m && cnt[3] == m) {
12+
return 0;
13+
}
14+
int ans = n;
15+
for (int i = 0, j = 0; i < n; ++i) {
16+
cnt[t.find(s[i])]--;
17+
while (j <= i && cnt[0] <= m && cnt[1] <= m && cnt[2] <= m && cnt[3] <= m) {
18+
ans = min(ans, i - j + 1);
19+
cnt[t.find(s[j++])]++;
20+
}
21+
}
22+
return ans;
23+
}
24+
};

0 commit comments

Comments
 (0)