Skip to content

Commit dbdd6e3

Browse files
committed
feat: add solutions to lc problem: No.1100
No.1100.Find K-Length Substrings With No Repeated Characters
1 parent 17f5f79 commit dbdd6e3

File tree

6 files changed

+212
-2
lines changed

6 files changed

+212
-2
lines changed

solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,96 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
固定大小的滑动窗口。
43+
4244
<!-- tabs:start -->
4345

4446
### **Python3**
4547

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

4850
```python
49-
51+
class Solution:
52+
def numKLenSubstrNoRepeats(self, s: str, k: int) -> int:
53+
ans = j = 0
54+
mp = {}
55+
for i, c in enumerate(s):
56+
if c in mp:
57+
j = max(j, mp[c] + 1)
58+
mp[c] = i
59+
if i - j + 1 >= k:
60+
ans += 1
61+
return ans
5062
```
5163

5264
### **Java**
5365

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

5668
```java
69+
class Solution {
70+
public int numKLenSubstrNoRepeats(String s, int k) {
71+
int ans = 0;
72+
Map<Character, Integer> mp = new HashMap<>();
73+
for (int i = 0, j = 0; i < s.length(); ++i) {
74+
char c = s.charAt(i);
75+
if (mp.containsKey(c)) {
76+
j = Math.max(j, mp.get(c) + 1);
77+
}
78+
mp.put(c, i);
79+
if (i - j + 1 >= k) {
80+
++ans;
81+
}
82+
}
83+
return ans;
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int numKLenSubstrNoRepeats(string s, int k) {
94+
int ans = 0;
95+
unordered_map<int, int> mp;
96+
for (int i = 0, j = 0; i < s.size(); ++i)
97+
{
98+
char c = s[i];
99+
if (mp.count(c)) j = max(j, mp[c] + 1);
100+
mp[c] = i;
101+
if (i - j + 1 >= k) ++ans;
102+
}
103+
return ans;
104+
}
105+
};
106+
```
57107
108+
### **Go**
109+
110+
```go
111+
func numKLenSubstrNoRepeats(s string, k int) int {
112+
mp := make(map[rune]int)
113+
ans, j := 0, 0
114+
for i, c := range s {
115+
if v, ok := mp[c]; ok {
116+
j = max(j, v+1)
117+
}
118+
mp[c] = i
119+
if i-j+1 >= k {
120+
ans++
121+
}
122+
}
123+
return ans
124+
}
125+
126+
func max(a, b int) int {
127+
if a > b {
128+
return a
129+
}
130+
return b
131+
}
58132
```
59133

60134
### **...**

solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/README_EN.md

+73-1
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,85 @@ Notice K can be larger than the length of S. In this case is not possible to fin
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def numKLenSubstrNoRepeats(self, s: str, k: int) -> int:
58+
ans = j = 0
59+
mp = {}
60+
for i, c in enumerate(s):
61+
if c in mp:
62+
j = max(j, mp[c] + 1)
63+
mp[c] = i
64+
if i - j + 1 >= k:
65+
ans += 1
66+
return ans
5767
```
5868

5969
### **Java**
6070

6171
```java
72+
class Solution {
73+
public int numKLenSubstrNoRepeats(String s, int k) {
74+
int ans = 0;
75+
Map<Character, Integer> mp = new HashMap<>();
76+
for (int i = 0, j = 0; i < s.length(); ++i) {
77+
char c = s.charAt(i);
78+
if (mp.containsKey(c)) {
79+
j = Math.max(j, mp.get(c) + 1);
80+
}
81+
mp.put(c, i);
82+
if (i - j + 1 >= k) {
83+
++ans;
84+
}
85+
}
86+
return ans;
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
int numKLenSubstrNoRepeats(string s, int k) {
97+
int ans = 0;
98+
unordered_map<int, int> mp;
99+
for (int i = 0, j = 0; i < s.size(); ++i)
100+
{
101+
char c = s[i];
102+
if (mp.count(c)) j = max(j, mp[c] + 1);
103+
mp[c] = i;
104+
if (i - j + 1 >= k) ++ans;
105+
}
106+
return ans;
107+
}
108+
};
109+
```
62110
111+
### **Go**
112+
113+
```go
114+
func numKLenSubstrNoRepeats(s string, k int) int {
115+
mp := make(map[rune]int)
116+
ans, j := 0, 0
117+
for i, c := range s {
118+
if v, ok := mp[c]; ok {
119+
j = max(j, v+1)
120+
}
121+
mp[c] = i
122+
if i-j+1 >= k {
123+
ans++
124+
}
125+
}
126+
return ans
127+
}
128+
129+
func max(a, b int) int {
130+
if a > b {
131+
return a
132+
}
133+
return b
134+
}
63135
```
64136

65137
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int numKLenSubstrNoRepeats(string s, int k) {
4+
int ans = 0;
5+
unordered_map<int, int> mp;
6+
for (int i = 0, j = 0; i < s.size(); ++i)
7+
{
8+
char c = s[i];
9+
if (mp.count(c)) j = max(j, mp[c] + 1);
10+
mp[c] = i;
11+
if (i - j + 1 >= k) ++ans;
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func numKLenSubstrNoRepeats(s string, k int) int {
2+
mp := make(map[rune]int)
3+
ans, j := 0, 0
4+
for i, c := range s {
5+
if v, ok := mp[c]; ok {
6+
j = max(j, v+1)
7+
}
8+
mp[c] = i
9+
if i-j+1 >= k {
10+
ans++
11+
}
12+
}
13+
return ans
14+
}
15+
16+
func max(a, b int) int {
17+
if a > b {
18+
return a
19+
}
20+
return b
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int numKLenSubstrNoRepeats(String s, int k) {
3+
int ans = 0;
4+
Map<Character, Integer> mp = new HashMap<>();
5+
for (int i = 0, j = 0; i < s.length(); ++i) {
6+
char c = s.charAt(i);
7+
if (mp.containsKey(c)) {
8+
j = Math.max(j, mp.get(c) + 1);
9+
}
10+
mp.put(c, i);
11+
if (i - j + 1 >= k) {
12+
++ans;
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def numKLenSubstrNoRepeats(self, s: str, k: int) -> int:
3+
ans = j = 0
4+
mp = {}
5+
for i, c in enumerate(s):
6+
if c in mp:
7+
j = max(j, mp[c] + 1)
8+
mp[c] = i
9+
if i - j + 1 >= k:
10+
ans += 1
11+
return ans

0 commit comments

Comments
 (0)