Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
longest-substring-with-at-least-k-repeating-characters.cpp 984 Bytes
一键复制 编辑 原始数据 按行查看 历史
// Time: O(26 * n) = O(n)
// Space: O(26) = O(1)
// Recursive solution.
class Solution {
public:
int longestSubstring(string s, int k) {
return longestSubstringHelper(s, k, 0, s.size());
}
private:
int longestSubstringHelper(const string& s, int k, int start, int end) {
vector<int> count(26);
for (int i = start; i < end; ++i) {
++count[s[i] - 'a'];
}
int max_len = 0;
for (int i = start; i < end;) {
while (i < end && count[s[i] - 'a'] < k) {
++i;
}
if (i == end) {
break;
}
int j = i;
while (j < end && count[s[j] - 'a'] >= k) {
++j;
}
if (i == start && j == end) {
return end - start;
}
max_len = max(max_len, longestSubstringHelper(s, k, i, j));
i = j;
}
return max_len;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助