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的更多相关文章

  1. 395. Longest Substring with At Least K Repeating Characters

    395. Longest Substring with At Least K Repeating Characters 我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  10. leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)

    只能说还是太菜,抄的网上大神的做法: idea: mask 的每一位代表该位字母够不够k次,够k次为0,不够为1 对于每一位将其视为起点,遍历至末尾,找到其最大满足子串T的下标max_idx,之后从m ...

随机推荐

  1. 使用GNU工具链进行嵌入式裸机开发

    Embedded-Programming-with-the-GNU-Toolchain Vijay Kumar B. vijaykumar@bravegnu.org 翻译整理:thammer gith ...

  2. ubuntu16.04查看软件的安装位置

    以chromium-browser为例 find命令 totoro@SWH:~$ sudo find / -name chromium-browser /usr/lib/chromium-browse ...

  3. Centos7下shell脚本添加开机自启动

    添加开机自启脚本,注意都需要用绝对路径 psubscribe.sh脚本中的内容: nohup /usr/bin/php -f /data/aliyun51015cn/redisChannel/psub ...

  4. nlogn 求最长上升子序列 LIS

    最近在做单调队列,发现了最长上升子序列O(nlogn)的求法也有利用单调队列的思想. 最长递增子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]& ...

  5. Linux下Nginx+多Tocat下的负载均衡环境的简单搭建

    本文主要分为四个部分: 1.Nginx的搭建:2.JDK+Tomcat的搭建:3.静态HTML的访问配置:4.负载均衡的配置 ===================================== ...

  6. java泛型中的E,K,V,T,U,S

    注释: java 泛型类型使用大写形式,且比较短,这是常见的 在java库中,使用变量 E 表示集合的元素类型 K 和 V 分别表示数据库表数据的键key和值value的类型 T(如果有需要还可以使用 ...

  7. 机器学习(2):Softmax回归原理及其实现

    Softmax回归用于处理多分类问题,是Logistic回归的一种推广.这两种回归都是用回归的思想处理分类问题.这样做的一个优点就是输出的判断为概率值,便于直观理解和决策.下面我们介绍它的原理和实现. ...

  8. String s="hello";s+="world";s变化了吗?原始的String对象的内容变了吗?

    分析: String s="hello";s+="world"; 引用变量s 一开始指向String对象("hello" :0x001); ...

  9. .NET程序员提高效率的70多个开发工具

    工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存管理,诊断工具等,涉及开发过程的各个环节, ...

  10. composer安装Workerman报错:Installation failed, reverting ./composer.json to its original content.

    今天想在TP5上安装workerman,实现一个后台消息提醒功能. 第一步就卡住了,根据手册里说的首先通过composer安装 $ composer require topthink/think-wo ...