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) such that every character in T appears no less than k times. Example 1: Input:
s = "aaabb", k = 3 Output:
3 The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2: Input:
s = "ababbc", k = 2 Output:
5 The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
Analysis:
Given a string s, find out all chars that are invalid (i.e., count < k). The longest substring must reside in one of the substrings divided by those invalid chars. We find out all those possible substrings and recursively address each of them.
NOTE: repeatedly using s.charAt() is actually very slow. So we convert the string to charArray in the first place
public class Solution {
public int longestSubstring(String s, int k) {
return longestSubstring(s.toCharArray(), 0, s.length()-1, k);
}
public int longestSubstring(char[] arr, int start, int end, int k) {
if (end < start) return 0;
if (end-start+1 < k) return 0;
int[] count = new int[26];
for (int i=start; i<=end; i++) {
count[arr[i]-'a']++;
}
for (int i=0; i<26; i++) {
if (count[i] == 0) continue;
if (count[i] < k) {
int j = start;
for (; j<=end; j++) {
if (arr[j] == (char)('a'+i)) break;
}
int left = longestSubstring(arr, start, j-1, k);
int right = longestSubstring(arr, j+1, end, k);
return Math.max(left, right);
}
}
return end-start+1;
}
}
Leetcode: 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 ...
- 395. Longest Substring with At Least K Repeating Characters
395. Longest Substring with At Least K Repeating Characters 我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为 ...
- [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 ...
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 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 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 ...
- 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 ...
随机推荐
- Web Service性能测试方案
目录: 1.web Service简介 2.SoapUI介绍 3.使用SoapUI进行web service性能测试 4.使用LR进行web service性能测试 5.使用JMeter进行web s ...
- shell执行mysql操作
http://ully.iteye.com/blog/1226494 http://www.jb51.net/article/55207.htm shell执行mysql操作 mysql -hhos ...
- IN()
High Performance My SQL, Third Edition Consider the followingWHERE clause: WHERE eye_color IN('brown ...
- sentinel
Computer Science An Overview _J. Glenn Brookshear _11th Edition Inherent in processing a sequential ...
- 已禁用对分布式事务管理器(MSDTC)的网络访问。请使用组件服务管理工具启用 DTC 以便在 MSDTC 安全配置中进行网络访问。
今天写ASP.NET程序,在网页后台的c#代码里写了个事务,事务内部对一张表进行批量插入,对另外一张表进行查询与批量插入. 结果第二张表查询后foreach迭代操作时报错:已禁用对分布式事务管理器(M ...
- mysql慢查询
查看当前服务器是否开启慢查询: 1.快速办法,运行sql语句show VARIABLES like "%slow%" 2.直接去my.conf中查看. my.conf中的配置(放在 ...
- 一种swift编码风格指南(供参考,by linkedin)
http://www.cocoachina.com/swift/20160701/16894.html
- HIVE中的HQL操作
1.字段查询 select empno,ename from emp; 2.过滤where,limit,distinct select * from emp where sal >2500; s ...
- 行高不设单位的好处 line-height:1.8
今天无意间看了到了line-height:1.8 感觉挺有意思的,然后翻了下行高的知识,发现还挺有文章的,不妨温故而知新. 先回顾下:顶线.中线.基线.底线 vertical-align是元素的垂直对 ...
- [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...