395. Longest Substring with At Least K Repeating Characters
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的更多相关文章
- [LeetCode] 395. 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 ...
- 2016/9/21 leetcode 解题笔记 395.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 ...
- LeetCode 395. Longest Substring with At Least K Repeating Characters C#
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- leetcode 395. 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 ...
- 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)
只能说还是太菜,抄的网上大神的做法: idea: mask 的每一位代表该位字母够不够k次,够k次为0,不够为1 对于每一位将其视为起点,遍历至末尾,找到其最大满足子串T的下标max_idx,之后从m ...
- 395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度.示例 1:输入:s = "aaabb", k = 3输出:3最 ...
- 【leetcode】395. Longest Substring with At Least K Repeating Characters
题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...
- [LeetCode] 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 ...
随机推荐
- zabbix 3.0 安装
zabbix3.0安装注意: 1.PHP要5.4版本以上 2.防火墙关闭 3.selinux关闭 注:本操作系统为centos 6.5 X86 操作步骤 一.安装PHP 添加 epel 源 # r ...
- tomcat 虚拟目录与显示目录中文件列表
虚拟目录: 该方法推荐使用,比较简单. 在%tomcat%\conf\Catalina\localhost(该目录可能需要手工创建)下新建一个文件abc.xml,注意文件名中的abc就表示虚拟目录的名 ...
- 网络子系统55_ip协议分片重组_加入ipq
//ip分片加入到正确的ipq结构 //调用路径:ip_defrag->ip_frag_queue // 处理过程: // 1.正在被释放的ipq,不处理新加入的分片(ipq正在被释放由last ...
- JQuery Mobile navbar动态刷新创建
今天突然用到须要动态改变tab页, 布局代码例如以下: <div data-role="navbar" id='divtab'> <ul id='divtabul ...
- mysqldump原理3
现网中数据库运维时,要经常对数据库做热备.为保证恢复时数据的完整性与一致性, 一种方法是在备份之前锁表,但锁表会影响正在运行的业务. mysqldump是当前MySQL中最常用的备份工具,通过mysq ...
- 学习笔记之DB2 9 Fundamentals 730
Sequence中cache的影响,每新建一个连接,next value值增加increment * cache.如果加上order,则会按顺序生成值. increment cache ; Conne ...
- [001]socket通信--server和client实现迭代的简单例子
server端: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unist ...
- GCC生成的汇编代码
假设我们写了一个C代码文件 code.c包含下面代码: int accum = 0; int sum(int x, int y){ int t = x + y; accum += t; return ...
- 对比AppScan Source和Fortify扫描AltoroJ的结果
对比AppScan Source和Fortify扫描AltoroJ的结果: http://blog.csdn.net/testing_is_believing/article/details/1963 ...
- 【安卓小技巧】WebView设置在本页面打开网页,而不是启动浏览器打开
使用WebView可以巧妙的在安卓APP中嵌入HTML页面, WebView wb = (WebView) findViewById(R.id.web); //找到WebView控件 wb.setWe ...