C++ 3ms divide solution.


  • 0
    M
    class Solution {
    	int longestSubstring(string& s, int k, int l, int r) {
    		if (r - l<k)return 0;
    		int cnt[26], start = l, res = 0;
    		memset(cnt, 0, sizeof(cnt));
    		for (int i = l;i<r;++i) ++cnt[s[i]];
    		for (int i = l;i<r;++i) {
    			if (cnt[s[i]]<k) {
    				res = max(longestSubstring(s, k, start, i), res);
    				start = i + 1;
    			}
    		}
    		if (start == l)return r - l;
    		return max(longestSubstring(s, k, start, r), res);
    	}
    public:
    	int longestSubstring(string s, int k) {
    		for (int i = 0;i<s.length();++i) s[i] -= 'a';
    		return longestSubstring(s, k, 0, s.length());
    	}
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.