标签:lse size turn solution public code res sub bst
1 class Solution 2 { 3 public: 4 int longestSubstring(string s, int k) 5 { 6 if(s.empty()) return 0; 7 vector<int> hash(26); 8 for(auto a : s) hash[a - ‘a‘] ++;//统计字符串中字符出现的次数 9 //看整个字符串出现的字符是否满足均至少为k 10 bool fullstring = true; 11 for(auto a : s) 12 { 13 if(hash[a - ‘a‘] > 0 && hash[a - ‘a‘] < k) fullstring = false; 14 } 15 if(fullstring == true) return s.size(); 16 17 int begin = 0,end = 0,result = 0; 18 while(end < s.size()) 19 { 20 if(hash[s[end] - ‘a‘] < k)//如果出现的次数小于k,则切割成[begin,end - 1]和[end + 1,...] 21 { 22 result = max(result,longestSubstring(s.substr(begin,end - begin),k)); 23 begin = end + 1; 24 } 25 end ++; 26 } 27 result = max(result,longestSubstring(s.substr(begin),k));//最后还要进行统计result 28 return result; 29 } 30 };
标签:lse size turn solution public code res sub bst
原文地址:https://www.cnblogs.com/yuhong1103/p/12793307.html