前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 0395 - Longest Substring with At Least K Repeating Characters

LeetCode 0395 - Longest Substring with At Least K Repeating Characters

作者头像
Reck Zhang
发布2021-08-11 11:08:55
2180
发布2021-08-11 11:08:55
举报
文章被收录于专栏:Reck Zhang

Longest Substring with At Least K Repeating Characters

Desicription

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:

代码语言:javascript
复制
Input:
s = "aaabb", k = 3

Output:
3

The longest substring is "aaa", as 'a' is repeated 3 times.

Example 2:

代码语言:javascript
复制
Input:
s = "ababbc", k = 2

Output:
5

The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.

Solution

代码语言:javascript
复制
class Solution {
public:
    int longestSubstring(const std::string& s, const int& k) {
        return longestSubstring(s, k, 0, static_cast<int>(s.size()) - 1);
    }

    int longestSubstring(const std::string& s, const int& k, int left, int right) {
        if(left > right) {
            return 0;
        }

        auto count = std::vector<int>(26, 0);
        for(int i = left; i <= right; i++) {
            count[s[i] - 'a']++;
        }

        int res = right - left + 1;
        for(int i = left; i <= right; i++) {
            if(count[s[i] - 'a'] > 0 && count[s[i] - 'a'] < k) {
                res = std::max(longestSubstring(s, k, left, i - 1), longestSubstring(s, k, i + 1, right));
                return res;
            }
        }
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-09-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Longest Substring with At Least K Repeating Characters
    • Desicription
      • Solution
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档