博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
395. Longest Substring with At Least K Repeating Characters
阅读量:6763 次
发布时间:2019-06-26

本文共 1374 字,大约阅读时间需要 4 分钟。

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.

Example 1:

Input:s = "aaabb", k = 3Output:3The longest substring is "aaa", as 'a' is repeated 3 times.

 

Example 2:

Input:s = "ababbc", k = 2Output:5The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.

 

class Solution {public:    int longestSubstring(string s, int k) {        int n = s.size();        return helper(s, 0, n-1, k);    }private:    // looking for longest string within index range [l, r]    int helper(string& s, int l, int r, int k) {        vector
mp(26, 0); for (int i = l; i <= r; i++) mp[s[i]-'a']++; // check whether the whole string meets requirement bool pass = true; for (int i = 0; i < 26 && pass; i++) { if (mp[i] && mp[i] < k) pass = false; } if (pass) return r-l+1; // using all characters with occurrence > 0 && < k to divide the string int i = l, ans = 0; for (int j = l; j <= r; j++) { if (mp[s[j]-'a'] && mp[s[j]-'a'] < k) { ans = max(ans, helper(s, i, j-1, k)); i = j+1; } } return max(ans, helper(s, i, r, k)); }};

 

 

转载于:https://www.cnblogs.com/jxr041100/p/8431659.html

你可能感兴趣的文章