1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public: int longestSubstring(string s, int k) { int n = s.size(), m[26] = {0}, left = 0, ans = 0; bool valid = true; for (char c : s) ++m[c - 'a']; for (int right = 0; right < n; ++right) if (m[s[right] - 'a'] < k) { ans = max(ans, longestSubstring(s.substr(left, right - left), k)); valid = false; left = right + 1; } return valid ? n : max(ans, longestSubstring(s.substr(left, n - left), k)); } };
|