File tree 6 files changed +212
-2
lines changed
solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters
6 files changed +212
-2
lines changed Original file line number Diff line number Diff line change 39
39
40
40
<!-- 这里可写通用的实现逻辑 -->
41
41
42
+ 固定大小的滑动窗口。
43
+
42
44
<!-- tabs:start -->
43
45
44
46
### ** Python3**
45
47
46
48
<!-- 这里可写当前语言的特殊实现逻辑 -->
47
49
48
50
``` 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
50
62
```
51
63
52
64
### ** Java**
53
65
54
66
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
67
56
68
``` 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
+ ```
57
107
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
+ }
58
132
```
59
133
60
134
### ** ...**
Original file line number Diff line number Diff line change @@ -53,13 +53,85 @@ Notice K can be larger than the length of S. In this case is not possible to fin
53
53
### ** Python3**
54
54
55
55
``` 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
57
67
```
58
68
59
69
### ** Java**
60
70
61
71
``` 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
+ ```
62
110
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
+ }
63
135
```
64
136
65
137
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments