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 ...
随机推荐
- Vue学习笔记进阶篇——Render函数
基础 Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template ...
- Ionic入门八:头部与底部
1.Header(头部) Header是固定在屏幕顶部的组件,可以包如标题和左右的功能按钮. ionic 默认提供了许多种颜色样式,你可以调用不同的样式名,当然也可以自定义一个. <div cl ...
- IOS常用第三方类库
开发几个常用的开源类库及下载地址: 1.json json编码解码 2.GTMBase64 base64编码解码 3.TouchXML xml解析 4.SFHFKeychainUtils 安全保存用户 ...
- ASP.NET MVC中在Action获取提交的表单数据方法
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- Am335x SPI 驱动测试
内核版本:3.14.65 CPU:Am335x 1.编译内核: make menuconfig Device Drivers -> <*>SPI support -> < ...
- Centos7下shell脚本添加开机自启动
添加开机自启脚本,注意都需要用绝对路径 psubscribe.sh脚本中的内容: nohup /usr/bin/php -f /data/aliyun51015cn/redisChannel/psub ...
- hdu 4536 dfs
题意:XCOM-Enemy Unknown是一款很好玩很经典的策略游戏.在游戏中,由于未知的敌人--外星人入侵,你团结了世界各大国家进行抵抗.随着游戏进展,会有很多的外星人进攻事件.每次进攻外星人会选 ...
- bzoj 1176
收获: 1.min, max, sum, 属于判定,等询问是”对象对答案贡献独立“,即不需要其他对象就了能更新答案,并保证只要所有对象更新过答案,那么该答案就是正确的.大概这就是所谓的”修改独立“. ...
- Python168的学习笔记4
关于普通文本文件的读写 python2.7中,未注明的字符都是以acsii来编码的,而要让字符能够通用,必须声明为unicode. s=u'你好',s.encode('utf8')就是指用utf8来进 ...
- js的继承实现方式
1. 使用call或者apply来实现js对象继承 function Animal(age){ this.age = age; this.say = function(){ console.log(' ...