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 ...
随机推荐
- thinkphp 5.0如何实现自定义404(异常处理)页面
404页面是客户端在浏览网页时,由于服务器无法正常提供信息,或是服务器无法回应,且不知道原因所返回的页面.404承载着用户体验与SEO优化的重任.404页面通常为用户访问了网站上不存在或已删除的页面, ...
- Mendeley文献管理软件使用介绍
<!DOCTYPE html> New Document /* GitHub stylesheet for MarkdownPad (http://markdownpad.com) / / ...
- GPL、BSD和Apache开源许可证
参考资料 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT) 如何选择开源许可证? - 阮一峰的网络日志 开源许可证教程 - 阮一峰的网络日志 简介 自由软件许可证由FSF(Free ...
- 正规表达式 转 NFA C++
今天来为大家分享一个编译原理中用正规表达式转NFA的小程序 正规表达式就是类似正则一样的式子,例如:(a|b)*abb,最后应该转化为: 大致的处理流程为: 例子中的表达式:(a|b)*abb,|和* ...
- python 与 mongodb的交互
- 虚拟多Mac地址工具Multimac
虚拟多Mac地址工具Multimac Mac地址采用唯一标识标记网络的各种设备.在同一个时间内,Linux系统中的网卡只能使用一个Mac地址.在渗透测试中,为了隐藏自己的身份,往往需要以不同的Ma ...
- 【转】高效率编辑器VIM
最近实习的时候需要在服务器上做Debug,不得不用到vim的相关操作.以前对vim这种被码农无数赞扬的神器望而却步,但今天试了之后感觉还是不错的.以后争取少用鼠标,少用insert模式. 这是从网上看 ...
- 命令神器:lsof 常用
lsof -i 显示所有网络连接lsof -i 6 获取IPv6信息lsof -itcp 显示tcp连接lsof -i:80 显示指定端口信息lsof -i@172.12.5.6 显示指定ip连接ls ...
- hdu 2819 记录路径的二分匹配
题目大意就是给出一个矩阵,每个格子里面要么是0, 要么是1:是否能够经过交换(交换行或者列)使得主对角线上都是1. 其实就行和列的匹配,左边是行,右边是列,然后如果行列交点是1,那么就可以匹配,看是否 ...
- Sql 先进先出计算积分
先建表,插入测试数据 --正积分表 CREATE table tb1 ( ) NOT NULL, ) NOT NULL, ) NULL, [point] [int] NULL ) ) ) ) ) ) ...