|
| 1 | +// Source : https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2016-09-08 |
| 4 | + |
| 5 | +/*************************************************************************************** |
| 6 | + * |
| 7 | + * Find the length of the longest substring T of a given string (consists of lowercase |
| 8 | + * letters only) such that every character in T appears no less than k times. |
| 9 | + * |
| 10 | + * Example 1: |
| 11 | + * |
| 12 | + * Input: |
| 13 | + * s = "aaabb", k = 3 |
| 14 | + * |
| 15 | + * Output: |
| 16 | + * 3 |
| 17 | + * |
| 18 | + * The longest substring is "aaa", as 'a' is repeated 3 times. |
| 19 | + * |
| 20 | + * Example 2: |
| 21 | + * |
| 22 | + * Input: |
| 23 | + * s = "ababbc", k = 2 |
| 24 | + * |
| 25 | + * Output: |
| 26 | + * 5 |
| 27 | + * |
| 28 | + * The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 |
| 29 | + * times. |
| 30 | + ***************************************************************************************/ |
| 31 | + |
| 32 | +const int NO_OF_CHARS = 256; |
| 33 | + |
| 34 | +/* if every character appears at least k times, the whole string is ok. |
| 35 | + * Otherwise split by a least frequent character. |
| 36 | + * |
| 37 | + * Because it will always be too infrequent and thus can't be part of any ok substring |
| 38 | + * and make the most out of the splits. |
| 39 | + */ |
| 40 | + |
| 41 | + |
| 42 | +class Solution { |
| 43 | +public: |
| 44 | + int longestSubstring(string s, int k) { |
| 45 | + |
| 46 | + //deal with edge cases |
| 47 | + if (s.size() == 0 || s.size() < k) return 0; |
| 48 | + if (k==1) return s.size(); |
| 49 | + |
| 50 | + //declare a map for every char's counter |
| 51 | + int count[NO_OF_CHARS]; |
| 52 | + memset(count , 0, sizeof(count)); |
| 53 | + |
| 54 | + //counting every char |
| 55 | + for (char ch : s) { |
| 56 | + count[ch]++; |
| 57 | + } |
| 58 | + |
| 59 | + int i=0; |
| 60 | + for ( i=0; i<NO_OF_CHARS; i++) { |
| 61 | + if (count[i] !=0 && count[i] < k) break; |
| 62 | + } |
| 63 | + //all of the chars meet the requirement |
| 64 | + if ( i >= NO_OF_CHARS ) return s.size(); |
| 65 | + |
| 66 | + // find the most infrequent char |
| 67 | + char least = 0; |
| 68 | + for (int c = 0; c < NO_OF_CHARS; c++) { |
| 69 | + if (count[c] == 0) continue; |
| 70 | + if (least == 0) { |
| 71 | + least = c; |
| 72 | + } else if ( count[c] < count[least]) { |
| 73 | + least = c; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + //split the string and run them recursively |
| 78 | + vector<string> subs; |
| 79 | + split(s, least, subs); |
| 80 | + |
| 81 | + int res = 0; |
| 82 | + for (string str: subs) { |
| 83 | + res = max(res, longestSubstring(str, k)); |
| 84 | + } |
| 85 | + return res; |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | +private: |
| 90 | + |
| 91 | + inline int max(int x, int y) { return x>y? x:y; } |
| 92 | + |
| 93 | + inline void split(const string &s, char delim, vector<string> &elems) { |
| 94 | + stringstream ss; |
| 95 | + ss.str(s); |
| 96 | + string item; |
| 97 | + while (getline(ss, item, delim)) { |
| 98 | + cout << item << endl; |
| 99 | + elems.push_back(item); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + inline vector<string> split(const string &s, char delim) { |
| 105 | + vector<string> elems; |
| 106 | + split(s, delim, elems); |
| 107 | + return elems; |
| 108 | + } |
| 109 | +}; |
0 commit comments