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.
Solution:
Map all characters in string s to a Hash map and store the count of characters as values.
if all characters occur more than k times, then return the whole string length as result;
 
if not, find the least occured character and split the string by this char, then call this function recursively 
to get the max length out of all substrings. 
return the max.
 
 public class Solution {
public int LongestSubstring(string s, int k) {
Dictionary<char,int> map = new Dictionary<char, int>();
if(string.IsNullOrEmpty(s))
{
return ;
}
int l = s.Length;
for(int i=; i<l; i++)
{
if(map.ContainsKey(s[i]))
{
map[s[i]]++;
}
else
{
map.Add(s[i],);
}
}
char minKey=map.Aggregate((p, r) => p.Value < r.Value ? p : r).Key;
if(map[minKey]>=k)
{
return l;
}
string[] splitted = s.Split(minKey);
int max = ;
foreach(string n in splitted)
{
int m = LongestSubstring(n,k);
if(m>max)
{
max = m;
}
}
return max; }
}
 

LeetCode 395. Longest Substring with At Least K Repeating Characters C#的更多相关文章

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

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

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

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

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

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

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

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

  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】395. Longest Substring with At Least K Repeating Characters

    题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...

  8. 395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串

    找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度.示例 1:输入:s = "aaabb", k = 3输出:3最 ...

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

随机推荐

  1. ShardedJedis实现学习

    ShardedJedis实现学习-我们到底能走多远系列(33) 我们到底能走多远系列(31) 扯淡: 工作是容易的赚钱是困难的 恋爱是容易的成家是困难的 相爱是容易的相处是困难的 决定是容易的可是等待 ...

  2. ASP.NET MVC2.0学习笔记:路由设置

    Route设置 在 <Professional in ASP.NET MVC2.0>一书的第四章,主要讲述了Route的简单设置.格式化设置.约束设置.区域路由.匹配文件.路由调试以及对R ...

  3. T-SQL查询语句(二):嵌套查询

    一个select...From...Where查询语句块可以嵌套在另一个select...From...Where查询块的Where子句中,称为嵌套查询.外层查询称为父查询,主查询.内层查询称为子查询 ...

  4. TaintDroid:智能手机监控实时隐私信息流跟踪系统(三)

    4.3   原生代码标记传播 Native 代码是不受TaintDroid监控的.理想情况下,我们获得了相同的传播语义当使用相同的解释副本时.因此,为了精确的在Java层进行污点监控,我们定义了两个必 ...

  5. Android 权限管理

    从 Android 6.0(API 级别 23)开始,用户开始在应用运行时向其授予权限,而不是在应用安装时授予.此方法可以简化应用安装过程,因为用户在安装或更新应用时不需要授予权限.它还让用户可以对应 ...

  6. .Net程序员学用Oracle系列(4):四个基本概念

    <.Net程序员学用Oracle系列:导航目录> 本文大纲 1.概念说明 1.1.表空间 1.2.SCHEMA 1.3.用户 1.4.权限 2.表空间管理 2.1.创建表空间 2.2.监控 ...

  7. Springboot 入门之Hello World

    首先使用maven进行包加载和配置,但是你maven一定要配置好,maven的setting.xml文件一定要配置好,不然jar包加载不了的. <project xmlns="http ...

  8. 点击某一按钮新增click,并切换页面

    应用场景:对于web端接收手机验证码的处理方法:1.如果有权限可以通过查询数据库来获得手机验证码,方便快捷.2.如果后台系统保存了手机验证码,可以去后台获取验证码,然后填写到前台页面,此方法有两种处理 ...

  9. asp.net mvc ActionResult

    定义在Controller中的Action方法大都返回一个ActionResult对象.ActionResult是对Action执行结果的封装,用于最终对请求进行响应.ASP.NET MVC提供了一系 ...

  10. 背水一战 Windows 10 (38) - 控件(布局类): Panel, Canvas, RelativePanel, StackPanel, Grid

    [源码下载] 背水一战 Windows 10 (38) - 控件(布局类): Panel, Canvas, RelativePanel, StackPanel, Grid 作者:webabcd 介绍背 ...