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
| func longestSubstring(s string, k int) int {
if len(s) == 0 || k > len(s) {
return 0
}
charCount := make(map[byte]int)
for i := 0; i < len(s); i++ {
charCount[s[i]]++
}
for char, count := range charCount {
if count < k {
maxLen := 0
substrs := strings.Split(s, string(char))
for _, substr := range substrs {
maxLen = max(maxLen, longestSubstring(substr, k))
}
return maxLen
}
}
return len(s)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
|