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 ...
随机推荐
- 【转载】在LoadRunner向远程Linux/Unix执行命令行并收集性能数据
前面介绍过在LoadRunner的Java协议实现“使用SSH连接Linux”,当然连接之后的故事由你主导. 今天要讲的,是一个非Java版本.是对“在LoadRunner中执行命令行程序之:pope ...
- locality
Computer Systems A Programmer's Perspective Second Edition Well-written computer programs tend to ex ...
- HybridTime - Accessible Global Consistency with High Clock Uncertainty
Amazon's Dynamo [9] and Facebook's Cassandra [13], relax the consistency model,and offer only eventu ...
- Lazarus解决无法识别中文路径的方法
procedure TForm1.Button1Click(Sender: TObject); var FileN:string; begin if self.OpenDialog1.Execute ...
- Inno Setup入门(十)——操作注册表 & 自启程序
http://379910987.blog.163.com/blog/static/3352379720110259414788/ 有些程序需要随系统启动,或者需要建立某些文件关联等问题,这些都是通过 ...
- ubuntu下opencv在Qt中的使用
1. 编译安装OpenCV2.4.9 本博已有文章描述 2. 安装Qt和QtCreator 从qt-project.org 下载Qt安装文件 qt-opensource-linux-x64-5.4. ...
- FW nexus docker
原文地址: http://www.cnblogs.com/wzy5223/p/5410990.html Nexus 3.0 可以创建三种docker仓库: 1. docker (proxy) ...
- HIVE中的几种排序
1.order by:全局排序 select * from emp order by sal; 2.sort by:对于每个reduce进行排序 set mapreduce.job.reduces=3 ...
- 一张表有三个字段:id(城市id) Cityname(城市名) Privence(所属省份)如果要统计每个省份有多少城市请用SQL实现。
一张表有三个字段:id(城市id) Cityname(城市名) Privence(所属省份)如果要统计每个省份有多少城市请用SQL实现.
- QcheckBox
#include "dialog.h" #include "ui_dialog.h" #include <QtCore> #include < ...