forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
395 Longest Substring with At Least K Repeating Characters
66 lines (50 loc) · 1.65 KB
/
395 Longest Substring with At Least K Repeating Characters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Leetcode 395: Longest Substring with At Least K Repeating Characters
Detailed Video Explanation: https://youtu.be/bHZkCAcj3dc
===================================================================
C++:
----
int longestSubstring(string s, int k) {
int n = s.length();
if(n == 0 || n < k) return 0;
if(k <= 1) return n;
unordered_map<char, int> counts;
for(char c: s) counts[c] += 1;
int l = 0;
while(l < n && counts[s[l]] >= k) l++;
if(l >= n-1) return l;
int ls1 = longestSubstring(s.substr(0, l), k);
while(l < n && counts[s[l]] < k) l++;
int ls2 = (l < n) ? longestSubstring(s.substr(l), k) : 0;
return max(ls1, ls2);
}
Java:
-----
public int longestSubstring(String s, int k) {
int n = s.length();
if(n == 0 || n < k) return 0;
if(k <= 1) return n;
Map<Character, Integer> counts = new HashMap();
for(char c: s.toCharArray())
counts.put(c, counts.getOrDefault(c, 0) + 1);
int l = 0;
while(l < n && counts.get(s.charAt(l)) >= k) l++;
if(l >= n-1) return l;
int ls1 = longestSubstring(s.substring(0, l), k);
while(l < n && counts.get(s.charAt(l)) < k) l++;
int ls2 = (l < n) ? longestSubstring(s.substring(l), k) : 0;
return Math.max(ls1, ls2);
}
Python3:
-------
def longestSubstring(self, s: str, k: int) -> int:
n = len(s)
if n == 0 or n < k: return 0
if k <= 1: return n
counts = Counter(s)
l = 0
while l < n and counts[s[l]] >= k: l += 1
if l >= n-1: return l
ls1 = self.longestSubstring(s[0:l], k)
while l < n and counts[s[l]] < k: l += 1
ls2 = self.longestSubstring(s[l:], k) if l<n else 0
return max(ls1, ls2)