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 ...
随机推荐
- 从经典问题来看 Copy 方法(转)
来自:Gua | 瓜地 链接:https://desgard.com/copy/ 在初学 iOS 的时候,可能会被灌输这么一个常识,切记 NSString 的 property 的修饰变量要写作 c ...
- Objective-C中的Block(闭包) (轉載)
来源: 伯乐在线 - 青玉伏案 链接:http://ios.jobbole.com/83229/ 学习OC有接触到一个新词Block(个人感觉又是一个牛气冲天的词),但不是新的概念,不是新的东西.学过 ...
- Asp.net中使用资源文件实现网站多语言
首先需要新建一个ASP.NET Web Application.然后右键项目文件Add->Add ASP.NET Folder->App-GlobalResources. 新建好资源文件夹 ...
- 网络防火墙实战-基于pfsense(2)
安装虚拟机 本博客所有内容是原创,如果转载请注明来源 http://blog.csdn.net/myhaspl/
- cocos2d-x3.1.1 step by step 学习笔记1----- 软件的安装与HelloWord
1:软件安装 系统:WIn7 软件:VS2012.cocosd-x3.1.1.python2.6.7.doxygen <1>首先安装VS2012,安装也是傻瓜式安装, <2>然 ...
- ios开发——实战OC篇&SQLite3的实际应用
SQLite3的实际应用 前面的文章中介绍了SQlite,并且介绍了他的各种语法及使用方法. 但是没有正在项目中使用特,今天就开始做一个小小的实例,就是使用SQLite3来实现数据库的相应操作并且把他 ...
- debian下图形化管理工具的安装
MySQL GUI Tools是一套图形化桌面应用工具套装,可以用来管理MySQL服务器.该套装工具包含三个工具:MySQL QueryBrowser.MySQL Administrator和MySQ ...
- 0x800a1391-Microsoft Jscript "JSON未定义"
本人在进行调试代码是遇到以下问题: 在运行到var result = JSON.parse(data);这句时,报错:JSON未定义.如下图:
- 修改UILabel的行间距
在iOS开发中 有时候为了调整一些UI效果 我们需要调整UILabel之间的行间距: contentLabel.text:label上显示的文字内容; 5:label行间距; contentLab ...
- 关于jQuery源码中(function(window,undefined){//dosomething()})(window)写法解释
一.首先是最常见的闭包 (Closure) 范式自执行函数的写法,这里用匿名函数封装(构造块级作用域),避免了匿名函数内部的代码与外部之间发生冲突(如使用了相同的变量名). (function() { ...