File tree Expand file tree Collapse file tree 4 files changed +104
-0
lines changed
0340-Longest-Substring-with-At-Most-K-Distinct-Characters/cpp-0340 Expand file tree Collapse file tree 4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.13 )
2
+ project (cpp_0340 )
3
+
4
+ set (CMAKE_CXX_STANDARD 14 )
5
+
6
+ add_executable (cpp_0340 main.cpp )
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2019-02-23
4
+
5
+ #include < iostream>
6
+ #include < unordered_map>
7
+ #include < cassert>
8
+
9
+ using namespace std ;
10
+
11
+
12
+ // / Sliding Window
13
+ // / Time Complexity: O(n)
14
+ // / Space Complexity: O(1)
15
+ class Solution {
16
+ public:
17
+ int lengthOfLongestSubstringKDistinct (string s, int k) {
18
+
19
+ if (!k) return 0 ;
20
+
21
+ int l = 0 , r = -1 , res = 0 ;
22
+ unordered_map<char , int > freq;
23
+ while (r + 1 < s.size ()){
24
+
25
+ while (r + 1 < s.size () && ok (freq, s[r + 1 ], k))
26
+ freq[s[r + 1 ]] ++, r ++;
27
+ // assert(freq.size() == k);
28
+ res = max (res, r - l + 1 );
29
+
30
+ while (l <= r && freq.size () >= k){
31
+ freq[s[l]] --;
32
+ if (freq[s[l]] == 0 ) freq.erase (s[l]);
33
+ l ++;
34
+ }
35
+ }
36
+ return res;
37
+ }
38
+
39
+ private:
40
+ bool ok (const unordered_map<char , int >& freq, char c, int k){
41
+
42
+ if (freq.size () <= k - 1 ) return true ;
43
+
44
+ assert (freq.size () == k);
45
+ return freq.count (c);
46
+ }
47
+ };
48
+
49
+
50
+ int main () {
51
+
52
+ return 0 ;
53
+ }
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2019-02-23
4
+
5
+ #include < iostream>
6
+ #include < unordered_map>
7
+ #include < cassert>
8
+
9
+ using namespace std ;
10
+
11
+
12
+ // / Sliding Window (Another implement version)
13
+ // / Time Complexity: O(n)
14
+ // / Space Complexity: O(1)
15
+ class Solution {
16
+ public:
17
+ int lengthOfLongestSubstringKDistinct (string s, int k) {
18
+
19
+ if (!k) return 0 ;
20
+
21
+ int l = 0 , r = -1 , res = 0 ;
22
+ unordered_map<char , int > freq;
23
+ while (r + 1 < s.size ()){
24
+
25
+ if (freq.size () < k || freq.count (s[r + 1 ]))
26
+ freq[s[r + 1 ]] ++, r ++;
27
+ else {
28
+ freq[s[l]] --;
29
+ if (freq[s[l]] == 0 ) freq.erase (s[l]);
30
+ l ++;
31
+ }
32
+
33
+ res = max (res, r - l + 1 );
34
+ }
35
+ return res;
36
+ }
37
+ };
38
+
39
+
40
+ int main () {
41
+
42
+ return 0 ;
43
+ }
Original file line number Diff line number Diff line change @@ -299,6 +299,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
299
299
| | | | | | |
300
300
| 337 | [ House Robber III] ( https://leetcode.com/problems/house-robber-iii/description/ ) | [ 无] | [ C++] ( 0337-House-Robber-III/cpp-0337/ ) | | |
301
301
| | | | | | |
302
+ | 340 | [ Longest Substring with At Most K Distinct Characters] ( https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ ) | [ solution] ( https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/solution/ ) | [ C++] ( 0340-Longest-Substring-with-At-Most-K-Distinct-Characters/cpp-0340/ ) | | |
303
+ | | | | | | |
302
304
| 343 | [ Integer Break] ( https://leetcode.com/problems/integer-break/description/ ) | [ 无] | [ C++] ( 0343-Integer-Break/cpp-0343/ ) | [ Java] ( 0343-Integer-Break/java-0343/src/ ) | |
303
305
| 344 | [ Reverse String] ( https://leetcode.com/problems/reverse-string/description/ ) | [ 无] | [ C++] ( 0344-Reverse-String/cpp-0344/ ) | | |
304
306
| 345 | [ Reverse Vowels of a String] ( https://leetcode.com/problems/reverse-vowels-of-a-string/description/ ) | [ 无] | [ C++] ( 0345-Reverse-Vowels-of-a-String/cpp-0345/ ) | | |
You can’t perform that action at this time.
0 commit comments