longest-substring-with-at-least-k-repeating-characters
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
public class Solution {
public int longestSubstring(String s, int k) {
// Find count of each char
HashMap mp = new HashMap();
Object intObj;
for (int i=0; i<s.length(); i++) {
char ch = s.charAt(i);
intObj = mp.remove(ch);
int st = 0;
if (intObj != null) {
st = (int) intObj;
}
st++;
mp.put(ch, st);
}
// prepre iterate secondly
int ret = 0;
int last = -1;
HashMap newMp = new HashMap();
// iterate secondly
for (int i=0; i<s.length(); i++) {
char ch = s.charAt(i);
int num = (int)mp.get(ch);
// pass if num fits
if (num >= k) {
intObj = newMp.get(ch);
int newNum = 0;
if (intObj != null) {
newNum = (int) intObj;
}
newNum++;
newMp.put(ch, newNum);
continue;
}
// handle if meets nofit char
Set filter = new HashSet();
Iterator iter = newMp.entrySet().iterator();
Map.Entry entry;
// check newMp and prepare filter
while (iter.hasNext()) {
entry = (Map.Entry) iter.next();
char cch = (char)entry.getKey();
int cnt = (int)entry.getValue();
if (cnt < k) {
filter.add(cch);
}
int allCnt = (int)mp.remove(cch);
allCnt -= cnt;
mp.put(cch, allCnt);
}
// Prune
if (filter.size() == newMp.size()) {
last = i;
newMp.clear();
continue;
}
// use filter to check each segment
HashMap fMp = new HashMap();
int newLast = last;
for (int j=last+1; j<=i; j++) {
char fch = ' ';
if (j < i) {
fch = s.charAt(j);
}
// need to check segment
if (j == i || filter.contains(fch)) {
iter = fMp.entrySet().iterator();
// check map of each segment
while (iter.hasNext()) {
entry = (Map.Entry) iter.next();
char ffch = (char)entry.getKey();
int fcnt = (int)entry.getValue();
// Prepare Prune by update newMp
int newCnt = (int)newMp.remove(ffch);
newCnt -= fcnt;
newMp.put(ffch, newCnt);
if (newCnt < k) {
filter.add(ffch);
}
}
newLast = j;
fMp.clear();
// Check Prune
if (filter.size() == newMp.size()) {
break;
}
}
// no need to check segment, pass
else {
intObj = fMp.get(fch);
int fNum = 0;
if (intObj != null) {
fNum = (int) intObj;
}
fNum++;
fMp.put(fch, fNum);
if (fNum >= k) {
iter = fMp.entrySet().iterator();
boolean isFit = true;
while (iter.hasNext()) {
entry = (Map.Entry) iter.next();
int cfcnt = (int)entry.getValue();
if (cfcnt < k) {
isFit = false;
break;
}
}
if (isFit) {
if (j-newLast > ret) {
ret = j-newLast;
}
}
}
}
}
newMp.clear();
last = i;
}
if (s.length()-last-1 > ret) {
ret = s.length()-last-1;
}
return ret;
}
}
下面那个有未考虑到的地方,比如:
aaabbcbdcc
3
下面得出0,其实应该是3.
public class Solution {
public int longestSubstring(String s, int k) {
// Find count of each char
HashMap mp = new HashMap();
Object intObj;
for (int i=0; i<s.length(); i++) {
char ch = s.charAt(i);
intObj = mp.remove(ch);
int st = 0;
if (intObj != null) {
st = (int) intObj;
}
st++;
mp.put(ch, st);
} // prepre iterate secondly
int ret = 0;
int last = -1;
HashMap newMp = new HashMap(); // iterate secondly
for (int i=0; i<s.length(); i++) {
char ch = s.charAt(i);
int num = (int)mp.get(ch); // pass if num fits
if (num >= k) {
intObj = newMp.get(ch);
int newNum = 0;
if (intObj != null) {
newNum = (int) intObj;
}
newNum++;
newMp.put(ch, newNum);
continue;
} // handle if meets nofit char
Set filter = new HashSet();
Iterator iter = newMp.entrySet().iterator();
Map.Entry entry; // check newMp and prepare filter
while (iter.hasNext()) {
entry = (Map.Entry) iter.next();
char cch = (char)entry.getKey();
int cnt = (int)entry.getValue(); if (cnt < k) {
filter.add(cch);
} int allCnt = (int)mp.remove(cch);
allCnt -= cnt;
mp.put(cch, allCnt);
} // Prune
if (filter.size() == newMp.size()) {
last = i;
newMp.clear();
continue;
} // use filter to check each segment
HashMap fMp = new HashMap();
int newLast = last;
for (int j=last+1; j<=i; j++) {
char fch = ' ';
if (j < i) {
fch = s.charAt(j);
} // need to check segment
if (j == i || filter.contains(fch)) {
iter = fMp.entrySet().iterator();
boolean fits = true; // check map of each segment
while (iter.hasNext()) {
entry = (Map.Entry) iter.next();
char ffch = (char)entry.getKey();
int fcnt = (int)entry.getValue(); if (fcnt < k) {
fits = false;
} // Prepare Prune by update newMp
int newCnt = (int)newMp.remove(ffch);
newCnt -= fcnt;
newMp.put(ffch, newCnt); if (newCnt < k) {
filter.add(ffch);
}
} // update final ret
if (fits) {
if (j-newLast-1 > ret) {
ret = j-newLast-1;
}
}
newLast = j;
fMp.clear(); // Check Prune
if (filter.size() == newMp.size()) {
break;
} }
// no need to check segment, pass
else {
intObj = fMp.get(fch);
int fNum = 0;
if (intObj != null) {
fNum = (int) intObj;
}
fNum++;
fMp.put(fch, fNum);
}
}
newMp.clear();
last = i; }
if (s.length()-last-1 > ret) {
ret = s.length()-last-1;
}
return ret;
}
} test case: "zzzzzzzzzzaaaaaaaaabbbbbbbbhbhbhbhbhbhbhicbcbcibcbccccccccccbbbbbbbbaaaaaaaaafffaahhhhhiaahiiiiiiiiifeeeeeeeeee"
10 expected return: 21
longest-substring-with-at-least-k-repeating-characters的更多相关文章
- 395. Longest Substring with At Least K Repeating Characters
395. Longest Substring with At Least K Repeating Characters 我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为 ...
- [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 ...
- [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 ...
- [Swift]LeetCode395. 至少有K个重复字符的最长子串 | 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 ...
- 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: 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
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 ...
随机推荐
- 在windows下的CLI模式下如何运行php文件
https://blog.csdn.net/evkj2013/article/details/52313728 https://jingyan.baidu.com/article/da1091fb09 ...
- modCount干嘛的
在ArrayList.LinkedList.HashMap等等的内部增删改中我们总能看到modCount的身影,modCount字面意思就是修改次数,但为什么要记录modCount的修改次数呢? 大家 ...
- React Native网络编程之Fetch
目录 1.前言 2.什么是Fetch 3.最简单的应用 4.支持的请求参数 - 4.1. 参数详讲 - 4.2. 示例 5.请求错误与异常处理 1. 前言 网络请求是开发APP中不可或缺的一部 ...
- mysql正则表达式,实现多个字段匹配多个like模糊查询
现在有这么一个需求 一个questions表,字段有题目(TestSubject),选项(AnswerA,AnswerB,AnswerC,AnswerD,AnswerE) 要求字段不包含png,jpg ...
- JDK源码分析(一)——ArrayList
目录 ArrayList分析 ArrayList继承结构 ArrayList字段属性 ArrayList构造函数 重要方法 ArrayList Iterator迭代器 总结 ArrayList分析 ...
- 大数据技术之_13_Azkaban学习_Azkaban(阿兹卡班)介绍 + Azkaban 安装部署 + Azkaban 实战
一 概述1.1 为什么需要工作流调度系统1.2 常见工作流调度系统1.3 各种调度工具特性对比1.4 Azkaban 与 Oozie 对比二 Azkaban(阿兹卡班) 介绍三 Azkaban 安装部 ...
- 从Table 表中取出第 m 条到第 n 条的记录
* FROM Table id FROM Table )) --从TABLE表中取出第m到n条记录 (Exists版本) * FROM TABLE AS a WHERE Not Exists ( * ...
- [JOISC2014]水筒
OJ题号: BZOJ4242.AtCoder-JOISC2014E 题目大意: 给你一个h*w的网格图,每个格子可能是空地.障碍物和建筑物. 你只可以从空地上走过或者从建筑物中穿过. 建筑物总共有p个 ...
- 【洛谷】1494:[国家集训队]小Z的袜子【莫队】
P1494 [国家集训队]小Z的袜子 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...