classSolution { public: intdfs(const string& s, int l, int r, int k){ vector<int> cnt(26, 0); for (int i = l; i <= r; i++) { cnt[s[i] - 'a']++; }
char split = 0; for (int i = 0; i < 26; i++) { if (cnt[i] > 0 && cnt[i] < k) { split = i + 'a'; break; } } if (split == 0) { return r - l + 1; }
int i = l; int ret = 0; while (i <= r) { while (i <= r && s[i] == split) { i++; } if (i > r) { break; } int start = i; while (i <= r && s[i] != split) { i++; }
int length = dfs(s, start, i - 1, k); ret = max(ret, length); } return ret; }
intlongestSubstring(string s, int k){ int n = s.length(); returndfs(s, 0, n - 1, k); } };
classSolution { publicintlongestSubstring(String s, int k) { intn= s.length(); return dfs(s, 0, n - 1, k); }
publicintdfs(String s, int l, int r, int k) { int[] cnt = newint[26]; for (inti= l; i <= r; i++) { cnt[s.charAt(i) - 'a']++; }
charsplit=0; for (inti=0; i < 26; i++) { if (cnt[i] > 0 && cnt[i] < k) { split = (char) (i + 'a'); break; } } if (split == 0) { return r - l + 1; }
inti= l; intret=0; while (i <= r) { while (i <= r && s.charAt(i) == split) { i++; } if (i > r) { break; } intstart= i; while (i <= r && s.charAt(i) != split) { i++; }
intlength= dfs(s, start, i - 1, k); ret = Math.max(ret, length); } return ret; } }
var longestSubstring = function(s, k) { const n = s.length; returndfs(s, 0, n - 1, k); }
constdfs = (s, l, r, k) => { const cnt = newArray(26).fill(0); for (let i = l; i <= r; i++) { cnt[s[i].charCodeAt() - 'a'.charCodeAt()]++; }
let split = 0; for (let i = 0; i < 26; i++) { if (cnt[i] > 0 && cnt[i] < k) { split = String.fromCharCode(i + 'a'.charCodeAt()); break; } } if (split == 0) { return r - l + 1; }
let i = l; let ret = 0; while (i <= r) { while (i <= r && s[i] === split) { i++; } if (i > r) { break; } let start = i; while (i <= r && s[i] !== split) { i++; }
const length = dfs(s, start, i - 1, k); ret = Math.max(ret, length); } return ret; };
intdfs(char* s, int l, int r, int k) { int cnt[26]; memset(cnt, 0, sizeof(cnt)); for (int i = l; i <= r; i++) { cnt[s[i] - 'a']++; }
char split = 0; for (int i = 0; i < 26; i++) { if (cnt[i] > 0 && cnt[i] < k) { split = i + 'a'; break; } } if (split == 0) { return r - l + 1; }
int i = l; int ret = 0; while (i <= r) { while (i <= r && s[i] == split) { i++; } if (i > r) { break; } int start = i; while (i <= r && s[i] != split) { i++; }
int length = dfs(s, start, i - 1, k); ret = fmax(ret, length); } return ret; }
classSolution { public: intlongestSubstring(string s, int k){ int ret = 0; int n = s.length(); for (int t = 1; t <= 26; t++) { int l = 0, r = 0; vector<int> cnt(26, 0); int tot = 0; int less = 0; while (r < n) { cnt[s[r] - 'a']++; if (cnt[s[r] - 'a'] == 1) { tot++; less++; } if (cnt[s[r] - 'a'] == k) { less--; }
while (tot > t) { cnt[s[l] - 'a']--; if (cnt[s[l] - 'a'] == k - 1) { less++; } if (cnt[s[l] - 'a'] == 0) { tot--; less--; } l++; } if (less == 0) { ret = max(ret, r - l + 1); } r++; } } return ret; } };
var longestSubstring = function(s, k) { let ret = 0; const n = s.length; for (let t = 1; t <= 26; t++) { let l = 0, r = 0; const cnt = newArray(26).fill(0); let tot = 0; let less = 0; while (r < n) { cnt[s[r].charCodeAt() - 'a'.charCodeAt()]++; if (cnt[s[r].charCodeAt() - 'a'.charCodeAt()] === 1) { tot++; less++; } if (cnt[s[r].charCodeAt() - 'a'.charCodeAt()] === k) { less--; }
while (tot > t) { cnt[s[l].charCodeAt() - 'a'.charCodeAt()]--; if (cnt[s[l].charCodeAt() - 'a'.charCodeAt()] === k - 1) { less++; } if (cnt[s[l].charCodeAt() - 'a'.charCodeAt()] === 0) { tot--; less--; } l++; } if (less == 0) { ret = Math.max(ret, r - l + 1); } r++; } } return ret; };
funclongestSubstring(s string, k int) (ans int) { for t := 1; t <= 26; t++ { cnt := [26]int{} total := 0 lessK := 0 l := 0 for r, ch := range s { ch -= 'a' if cnt[ch] == 0 { total++ lessK++ } cnt[ch]++ if cnt[ch] == k { lessK-- }
for total > t { ch := s[l] - 'a' if cnt[ch] == k { lessK++ } cnt[ch]-- if cnt[ch] == 0 { total-- lessK-- } l++ } if lessK == 0 { ans = max(ans, r-l+1) } } } return ans }
funcmax(a, b int)int { if a > b { return a } return b }
intlongestSubstring(char* s, int k) { int ret = 0; int n = strlen(s); for (int t = 1; t <= 26; t++) { int l = 0, r = 0; int cnt[26]; memset(cnt, 0, sizeof(cnt)); int tot = 0; int less = 0; while (r < n) { cnt[s[r] - 'a']++; if (cnt[s[r] - 'a'] == 1) { tot++; less++; } if (cnt[s[r] - 'a'] == k) { less--; }
while (tot > t) { cnt[s[l] - 'a']--; if (cnt[s[l] - 'a'] == k - 1) { less++; } if (cnt[s[l] - 'a'] == 0) { tot--; less--; } l++; } if (less == 0) { ret = fmax(ret, r - l + 1); } r++; } } return ret; }