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 ...
随机推荐
- ubuntu 安装 theano
参考博客: http://www.cnblogs.com/anyview/p/5025704.html 1. 安装gfortran, numpy, scipy, sklearn, blas, atla ...
- [CodeForces - 678F] Lena and Queries 线段树维护凸包
大致题意: 给出三种操作 1.往平面点集中添加一个点 2.删除第i次添加的点 3.给出一个q,询问平面点集中的q*x+y的最大值 首先对于每个询问,可将z=q*x+y转化为y=z-q*x,即过点(x, ...
- JavaQuery
1.初识jQuery <!DOCTYPE html> <html> <head lang="en"> <meta charse ...
- Django2.0中URL的路由机制
路由是关联url及其处理函数关系的过程.Django的url路由配置在settings.py文件中ROOT_URLCONF变量指定全局路由文件名称. Django的路由都写在urls.py文件中的ur ...
- centOS7下SVN的安装和使用
1. 安装 CentOS通过yum安装subversion. # yum install subversion subversion安装在/bin目录: # which svnserve #查看目录 ...
- NOIP2018 生气记
今年的题都不怎么难 只是考到的东西相当相当的奇怪... 不想写题解,写出来感觉只是伤心的事 .................... Day1 一进考场就感受到了比去年要严一些... 花了1小时30分 ...
- [LOJ#2980][THUSCH2017]大魔法师(线段树+矩阵)
每个线段树维护一个行向量[A,B,C,len]分别是这个区间的A,B,C区间和与区间长度,转移显然. 以及此题卡常,稍微哪里写丑了就能100->45. #include<cstdio> ...
- 利用Pastezort渗透win7
下载Pastezort git clone https://github.com/ZettaHack/PasteZort.git 给Pastezort文件夹提升权限 /root/PasteZort/ ...
- OpenGL ES 3.0 图元装配
1. 前言 之前已经把纹理的渲染给弄出来了,但是又遇到一个新的问题,那就是图元装配,比如说我已经把图片给显示出来了,但是呢,并没有做到让它显示到具体的位置,而跟这个位置相关的则需要靠图元装配. 图元装 ...
- python数据库操作——sqlite3模块
# -*- coding: utf-8 -*- ''' Version : Python27 Author : Spring God Date : 2012-4-26 ''' import sqlit ...