Skip to content

Commit 39a782c

Browse files
feat: add solutions to lc problem: No.0395 (doocs#782)
No.0395.Longest Substring with At Least K Repeating Characters Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent b4b86b1 commit 39a782c

File tree

4 files changed

+241
-2
lines changed

4 files changed

+241
-2
lines changed

solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,94 @@
4646
<!-- 这里可写当前语言的特殊实现逻辑 -->
4747

4848
```python
49+
class Solution:
50+
def longestSubstring(self, s: str, k: int) -> int:
51+
n = len(s)
52+
maxLength = 0
4953

54+
for i in range(1, 27):
55+
counter = dict()
56+
left = 0
57+
right = 0
58+
unique = 0
59+
kCount = 0
60+
61+
while right < n:
62+
if unique <= i:
63+
r = s[right]
64+
counter[r] = counter.get(r, 0) + 1
65+
66+
if counter[r] == 1:
67+
unique += 1
68+
if counter[r] == k:
69+
kCount += 1
70+
71+
right += 1
72+
73+
else:
74+
l = s[left]
75+
counter[l] = counter.get(l, 0) - 1
76+
77+
if counter[l] == 0:
78+
unique -= 1
79+
if counter[l] == k-1:
80+
kCount -= 1
81+
82+
left += 1
83+
84+
if unique == i and kCount == i:
85+
maxLength = max(maxLength, right-left)
86+
87+
return maxLength
5088
```
5189

5290
### **Java**
5391

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

5694
```java
57-
95+
class Solution {
96+
public int longestSubstring(String s, int k) {
97+
int maxLength = 0;
98+
int n = s.length();
99+
100+
for (int i = 1; i < 27; i++) {
101+
Map<Character, Integer> counter = new HashMap<>();
102+
int left = 0;
103+
int right = 0;
104+
int unique = 0;
105+
int kCount = 0;
106+
107+
while (right < n) {
108+
if (unique <= i) {
109+
char r = s.charAt(right);
110+
counter.put(r, counter.getOrDefault(r, 0) + 1);
111+
if (counter.get(r) == 1) {
112+
unique += 1;
113+
}
114+
if (counter.get(r) == k) {
115+
kCount += 1;
116+
}
117+
right += 1;
118+
} else {
119+
char l = s.charAt(left);
120+
counter.put(l, counter.getOrDefault(l, 2) - 1);
121+
if (counter.get(l) == 0) {
122+
unique -= 1;
123+
}
124+
if (counter.get(l) == k - 1) {
125+
kCount -= 1;
126+
}
127+
left += 1;
128+
}
129+
if (unique == i && kCount == i) {
130+
maxLength = Math.max(maxLength, right - left);
131+
}
132+
}
133+
}
134+
return maxLength;
135+
}
136+
}
58137
```
59138

60139
### **...**

solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,92 @@
3939
### **Python3**
4040

4141
```python
42+
class Solution:
43+
def longestSubstring(self, s: str, k: int) -> int:
44+
n = len(s)
45+
maxLength = 0
4246

47+
for i in range(1, 27):
48+
counter = dict()
49+
left = 0
50+
right = 0
51+
unique = 0
52+
kCount = 0
53+
54+
while right < n:
55+
if unique <= i:
56+
r = s[right]
57+
counter[r] = counter.get(r, 0) + 1
58+
59+
if counter[r] == 1:
60+
unique += 1
61+
if counter[r] == k:
62+
kCount += 1
63+
64+
right += 1
65+
66+
else:
67+
l = s[left]
68+
counter[l] = counter.get(l, 0) - 1
69+
70+
if counter[l] == 0:
71+
unique -= 1
72+
if counter[l] == k-1:
73+
kCount -= 1
74+
75+
left += 1
76+
77+
if unique == i and kCount == i:
78+
maxLength = max(maxLength, right-left)
79+
80+
return maxLength
4381
```
4482

4583
### **Java**
4684

4785
```java
48-
86+
class Solution {
87+
public int longestSubstring(String s, int k) {
88+
int maxLength = 0;
89+
int n = s.length();
90+
91+
for (int i = 1; i < 27; i++) {
92+
Map<Character, Integer> counter = new HashMap<>();
93+
int left = 0;
94+
int right = 0;
95+
int unique = 0;
96+
int kCount = 0;
97+
98+
while (right < n) {
99+
if (unique <= i) {
100+
char r = s.charAt(right);
101+
counter.put(r, counter.getOrDefault(r, 0) + 1);
102+
if (counter.get(r) == 1) {
103+
unique += 1;
104+
}
105+
if (counter.get(r) == k) {
106+
kCount += 1;
107+
}
108+
right += 1;
109+
} else {
110+
char l = s.charAt(left);
111+
counter.put(l, counter.getOrDefault(l, 2) - 1);
112+
if (counter.get(l) == 0) {
113+
unique -= 1;
114+
}
115+
if (counter.get(l) == k - 1) {
116+
kCount -= 1;
117+
}
118+
left += 1;
119+
}
120+
if (unique == i && kCount == i) {
121+
maxLength = Math.max(maxLength, right - left);
122+
}
123+
}
124+
}
125+
return maxLength;
126+
}
127+
}
49128
```
50129

51130
### **...**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public int longestSubstring(String s, int k) {
3+
int maxLength = 0;
4+
int n = s.length();
5+
6+
for (int i = 1; i < 27; i++) {
7+
Map<Character, Integer> counter = new HashMap<>();
8+
int left = 0;
9+
int right = 0;
10+
int unique = 0;
11+
int kCount = 0;
12+
13+
while (right < n) {
14+
if (unique <= i) {
15+
char r = s.charAt(right);
16+
counter.put(r, counter.getOrDefault(r, 0) + 1);
17+
if (counter.get(r) == 1) {
18+
unique += 1;
19+
}
20+
if (counter.get(r) == k) {
21+
kCount += 1;
22+
}
23+
right += 1;
24+
} else {
25+
char l = s.charAt(left);
26+
counter.put(l, counter.getOrDefault(l, 2) - 1);
27+
if (counter.get(l) == 0) {
28+
unique -= 1;
29+
}
30+
if (counter.get(l) == k - 1) {
31+
kCount -= 1;
32+
}
33+
left += 1;
34+
}
35+
if (unique == i && kCount == i) {
36+
maxLength = Math.max(maxLength, right - left);
37+
}
38+
}
39+
}
40+
return maxLength;
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution:
2+
def longestSubstring(self, s: str, k: int) -> int:
3+
n = len(s)
4+
maxLength = 0
5+
6+
for i in range(1, 27):
7+
counter = dict()
8+
left = 0
9+
right = 0
10+
unique = 0
11+
kCount = 0
12+
13+
while right < n:
14+
if unique <= i:
15+
r = s[right]
16+
counter[r] = counter.get(r, 0) + 1
17+
18+
if counter[r] == 1:
19+
unique += 1
20+
if counter[r] == k:
21+
kCount += 1
22+
23+
right += 1
24+
25+
else:
26+
l = s[left]
27+
counter[l] = counter.get(l, 0) - 1
28+
29+
if counter[l] == 0:
30+
unique -= 1
31+
if counter[l] == k-1:
32+
kCount -= 1
33+
34+
left += 1
35+
36+
if unique == i and kCount == i:
37+
maxLength = max(maxLength, right-left)
38+
39+
return maxLength

0 commit comments

Comments
 (0)