395. Longest Substring with At Least K Repeating Characters

时间:2022-09-18 19:25:45

395. Longest Substring with At Least K Repeating Characters

我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为分割符,然后不断切割,重复这个过程,我不知道运行时间,感觉会tle,然后交了,就ac了。

class Solution {
public:
int work(string &s, int k, int x, int y) {
if(y - x + 1 < k) return 0;
set<char> se;
vector<int> sum(26, 0);
for (int i = x; i <= y; i++) {
sum[s[i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if(sum[i] && sum[i] < k) {
se.insert(char('a' + i));
//cout << x << " " << y << " " << char('a' + i) << endl;
}
}
if(se.size() == 0) {
return y - x + 1;
}
int last = x;
int res = 0;
for (int i = x; i <= y; i++) {
if(se.count(s[i])) {
//cout << char(s[i]) << endl;
//cout << last << " " << i << endl;
if(i - 1 - last + 1 >= k) {
int t = work(s, k, last, i - 1);
res = max(res, t);
}
last = i + 1;
}
}
if(!se.count(s[y])) {
res = max(res, work(s, k, last, y));
}
return res;
}
int longestSubstring(string s, int k) {
return work(s, k, 0, s.size() - 1);
}
};

395. Longest Substring with At Least K Repeating Characters的更多相关文章

  1. &lbrack;LeetCode&rsqb; 395&period; Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  2. 2016&sol;9&sol;21 leetcode 解题笔记 395&period;Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  3. LeetCode 395&period; Longest Substring with At Least K Repeating Characters C&num;

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. leetcode 395&period; Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. 【LeetCode】395&period; Longest Substring with At Least K Repeating Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  6. leetcode 395&period; Longest Substring with At Least K Repeating Characters&lpar;高质量题&rpar;

    只能说还是太菜,抄的网上大神的做法: idea: mask 的每一位代表该位字母够不够k次,够k次为0,不够为1 对于每一位将其视为起点,遍历至末尾,找到其最大满足子串T的下标max_idx,之后从m ...

  7. 395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串

    找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度.示例 1:输入:s = "aaabb", k = 3输出:3最 ...

  8. 【leetcode】395&period; Longest Substring with At Least K Repeating Characters

    题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...

  9. &lbrack;LeetCode&rsqb; Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

随机推荐

  1. mysql 索引查询的问题

    之前碰到过一个问题,本来数据量不大的一张表,查询结果反应特别慢,不知道是什么原因. 后来才得知,查询的反应速度与结果集大小有关.  结果集越小反应速度越快.

  2. 一个简单的零配置命令行HTTP服务器

    http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs. 如果你不想重复的写 nodeJs 的 web-server.js, 则可以使用这个. 安装 (全局安装加 -g) ...

  3. 第四节:Vue表单标签和组件的基本用法,父子组件间的通信

    vue表单标签和组件的基本用法,父子组件间的通信,直接看例子吧. <!DOCTYPE html> <html> <head> <meta charset=&q ...

  4. 聊聊IO多路复用之select、poll、epoll详解

    本文转载自: http://mp.weixin.qq.com/s?__biz=MzAxODI5ODMwOA==&mid=2666538922&idx=1&sn=e6b436ef ...

  5. jsp之间传参中文乱码问题

    jsp页面之间传参,传中文会出现乱码问题. 如下: $('.yzjjfa_row').eq(0).append('<a class="yzjjfa_contItem jjfa_acti ...

  6. winpcap 发送接收速率

    总体情况: 在不修改winpcap源码的情况下,发包.收包最大速率3包/ms. 收包几个api的速率: 1. m_fp = pcap_open_live(adapter->name, 65536 ...

  7. InnoDB的约束机制

    数据完整性 关系型数据库系统和文件系统的一个不同点是,关系数据库本身能保证存储数据的完整性,不需要应用程序的控制,而文件系统一般需要在程序端进行控制.几乎所有的关系型数据库都提供了约束(constra ...

  8. PS 滤镜——极坐标变换到平面坐标

    %%% 极坐标到平面坐标 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorith ...

  9. MVC object htmlAttributes,IDictionary&lt&semi;string&comma; object&gt&semi; htmlAttributes 写法

    MVC object htmlAttributes:new {style="color:red",width="12px",height="10px& ...

  10. 【PAT】B1007 素数对猜想

    素数筛筛出规定范围内的所有素数存入数组 遍历数组,如果满足于后边的差2,计数器加加 #include <cstdio> const int maxn = 10000001; int pri ...