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 ...
随机推荐
- Datatable的Select()方法简介
DataTable是我们在进行开发时经常用到的一个类,并且经常需要对DataTable中的数据进行筛选等操作,下面就介绍一下Datatable中经常用到的一个方法——Select,微软提供了四个函数的 ...
- android studio里的build.gradle基本属性
//声明是android 程序 apply plugin: 'com.android.application' android { //编译SDK版本 compileSdkVersion 23 // ...
- XMPP——Smack[5]文件传输及离线消息的获取
三天时间,赶在最后一下午实现了文件的传输,本来需要实现离线文件的发送的,一直没想好怎么弄,找openfire的离线文件插件没找到,后来想出一种方法,起服务器时起了一个系统用户,一直在线,当用户发送离线 ...
- tableview 上拉时 标题行出现在顶部不动效果
类似这种效果: 其实很简单,利用tableview 的plain属性,然后使用section,其实滑上去不动的是 section的headView. -(NSInteger)numberOfSect ...
- pip和easy_install使用方式
easy_install 跟 pip 都是 Python 的套件管理程式,有了它們,在使用 Python 開發程式的時候會帶來不少方便. easy_install 和 pip 有什麼不一樣?據 pip ...
- 重现PHP Core的调用栈
以前, 我曾经介绍过如何通过PHP的Core文件获取信息:如何调试PHP的Core之获取基本信息, 对于调用参数这块, 当时介绍的获取方法比较复杂. 于是今天我为PHP 5.4的.gdbini ...
- Linux性能优化--CPU[备忘]
http://kumu-linux.github.io/blog/2014/04/21/performance-cpu/
- Pitfalls of the Hibernate Second-Level / Query Caches--reference
This post will go through how to setup the Hibernate Second-Level and Query caches, how they work an ...
- SQL Server 内存管理
windows memory: Memory: Cache Bytes 是系统的working set, 也就是系统使用的物理内存数目. 可以观察Windows用了多少物理内存. 1. System ...
- 【思考】由安装zabbix至排障php一系列引发的思考
[思考]由安装zabbix至排障php一系列引发的思考 linux的知识点林立众多,很有可能你在排查一个故障的时候就得用到另一门技术的知识: 由于linux本身的应用依赖的库和其它环境环环相扣,但又没 ...