Longest Substring with At Least K Repeating Characters Solutions in C++
Number 395
Difficulty Medium
Acceptance 41.5%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/// Author : Hao Chen// Date : 2016-09-08const int NO_OF_CHARS = 256;/* if every character appears at least k times, the whole string is ok.* Otherwise split by a least frequent character.** Because it will always be too infrequent and thus can't be part of any ok substring* and make the most out of the splits.*/class Solution {public:int longestSubstring(string s, int k) {//deal with edge casesif (s.size() == 0 || s.size() < k) return 0;if (k==1) return s.size();//declare a map for every char's counterint count[NO_OF_CHARS];memset(count , 0, sizeof(count));//counting every charfor (char ch : s) {count[ch]++;}int i=0;for ( i=0; i<NO_OF_CHARS; i++) {if (count[i] !=0 && count[i] < k) break;}//all of the chars meet the requirementif ( i >= NO_OF_CHARS ) return s.size();// find the most infrequent charchar least = 0;for (int c = 0; c < NO_OF_CHARS; c++) {if (count[c] == 0) continue;if (least == 0) {least = c;} else if ( count[c] < count[least]) {least = c;}}//split the string and run them recursivelyvector<string> subs;split(s, least, subs);int res = 0;for (string str: subs) {res = max(res, longestSubstring(str, k));}return res;return 0;}private:inline int max(int x, int y) { return x>y? x:y; }inline void split(const string &s, char delim, vector<string> &elems) {stringstream ss;ss.str(s);string item;while (getline(ss, item, delim)) {cout << item << endl;elems.push_back(item);}}inline vector<string> split(const string &s, char delim) {vector<string> elems;split(s, delim, elems);return elems;}};