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

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

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

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

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

  5. LeetCode "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

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

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

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

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

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

随机推荐

  1. ContentType Office

    Office对应ContentType 当从浏览器返回一个文件时,需要指定ContentType,以下是Office2007对应的值: "application/vnd.openxmlfor ...

  2. LR中Vugen的多进程与多线程(脚本命令行)

    Controller使用驱动程序(如mdrv.exe或r3vuser.exe)来运行Vuser.用户可以在Controller的run-time setting中选择Vuser的运行方式:多进程/多线 ...

  3. Supesite 参数说明

    supesite有人看到的是强大的功能,我看到的是坑爷的一些用法,第一次看到block,我晕了.对于参数一头雾水,下面收集了一些,备用吧. supesite标签调用参数详解 参数: blocktype ...

  4. BLE-NRF51822教程-RSSI获取

    当手机和设备连接上后,设备端可以通过获取RSSI,在一定程度上判断手机离设备的相对距离的远近. 获取函数很简单直接调用sd_ble_gap_rssi_get 接口函数就行了,传入连接句柄和buff就能 ...

  5. SVN hooks强制提交时填写日志

    #!/bin/bash REPOS="$1" TXN="$2" #svnlook路径 SVNLOOK=/usr/bin/svnlook #通过svnlook获取 ...

  6. JavaScipt选取文档元素的方法

    摘自JavaScript权威指南(jQuery根据样式选择器查找元素的终极方式是 先用getElementsByTagName(*)获取所有DOM元素,然后根据样式选择器对所有DOM元素进行筛选) 选 ...

  7. [LeetCode]题解(python):054-Spiral Matrix

    题目来源 https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n column ...

  8. [LeetCode]题解(python):052-N-Queens II

    题目来源 https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead outputt ...

  9. dedecms程序给栏目增加缩略图的方法

    用织梦程序做网站,有时候因为功能需求,我们要为网站的栏目页添加缩略图功能,而dedecms又没自带这个功能,那么就需要我们来修改程序了. 这里有一个栏目添加缩略图的方法,供大家参考. 涉及到文件如下( ...

  10. iOS开发中(null)与<null>的判断

    判断(null): if(m_result==nil) {      NSLog(@"KDA!"); } 判断<null>: if([m_result isEqual: ...