题目要求

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 = 3Output:
3The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:Input:
s = "ababbc", k = 2Output:
5The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.

找出字符串中的最长子字符串,满足该子字符串中任何字符出现的次数都大于k。

思路和代码

这是一个经典的分治法解决的问题,关键在于我们如何将这个问题分解为更小的子问题。反过来想,这个子字符串一定不包含什么元素呢?当一个元素出现的总次数小于k,那么该元素一定不在子字符串中,只需要将其作为分界点,分别找出左半部分和右半部分的满足条件的最长子字符串。

    public int longestSubstring(String s, int k) {return longestSubstring(s, k, 0, s.length()-1);}public int longestSubstring(String s, int k, int left, int right) {if(left > right) {return 0;}int[] count = new int[26];for(int i = left ; i<=right ; i++) {count[s.charAt(i) - 'a']++;}int result = right - left + 1;for(int i = left ; i<=right ; i++) {if(count[s.charAt(i)-'a'] < k && count[s.charAt(i)-'a'] > 0) {int leftLongest = longestSubstring(s, k, left, i-1);int rightLongest = longestSubstring(s, k, i+1, right);result = Math.max(leftLongest, rightLongest);break;}}return result;}

395. Longest Substring with At Least K Repeating Characters相关推荐

  1. leetcode 395. Longest Substring with At Least K Repeating Characters| 395. 至少有 K 个重复字符的最长子串(分治法)

    题目 https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ 题解 参考:官方题解 ...

  2. LeetCode Longest Substring with At Least K Repeating Characters(递归)

    问题:给出一个字符串s,要求求出最长的子串,每个字符出现至少k次 思路: 方法一,先统计每个字符出现的次数,然后分别从头和尾找出出现资料最小为k的位置,然后再此区间遍历,当出现小于k次时,在两个子区间 ...

  3. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  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. Longest Substring With At Most K Distinct Characters

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  6. 386 · Longest Substring with At Most K Distinct Characters最多有k个不同字符的最长子字符串

    链接:https://www.lintcode.com/problem/386/description https://mp.weixin.qq.com/s?__biz=MzU2OTUyNzk1NQ= ...

  7. [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  8. LeetCode 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法

    题目地址:Longest Substring with At Most Two Distinct Characters - LeetCode Given a string s , find the l ...

  9. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

最新文章

  1. hdu 4639 2013多校第四场 hehe Fibonacci 数列,组合计数,字符串处理
  2. 一个串口接2个设备_重庆市有2个大观镇,一个乡村旅游发达,一个特产柚子
  3. mysql 正则替换 换行,MySQL中使用replace、regexp进行正则表达式替换的用法分析
  4. MessagePack Jackson 数据大小
  5. 如何从SQL Server中的SELECT语句更新
  6. 使用Java语言开发微信公众平台(五)——获取access_token
  7. C# Socket tcp 发送数据大小问题
  8. CSS:修改输入框placeholder样式
  9. 当在浏览器中输入 Google.com 并按下回车之后发生了什么?
  10. 邮件传输的过程都看不懂。那我走(狗头)
  11. 开关造成的毛刺_令人困扰的DAC输出毛刺消灭记
  12. Madgwick AHRS算法笔记
  13. Nginx正则表达式与location匹配简介
  14. 第14章 项目采购管理
  15. Typescript泛型与implements子句
  16. 常用request方法
  17. hive sql 分组随机取数
  18. golang并发编程-04-通道-02-定时器、断续器
  19. 计算机英语的英文参考文献,计算机英文参考文献翻译英语论文mmwq8yrl.doc
  20. IntelliJ Idea 常用10款插件

热门文章

  1. mysql编写函数 求1 n 偶数之和,编写求1 2 3 - n的函数.在main函数中调用该函数
  2. button按钮onclick触发不了_单按钮启停:测试模拟脉冲发生器的动作
  3. vb.net限制软件使用次数_新增投屏及倍数播放,这款软件iOSAndroid全都有,影视神器,抓紧体验...
  4. python将csv文件导入mysql-使用python将csv文件导入Mysql数据库
  5. win10兼容模式怎么设置_win10护眼模式关不掉怎么办
  6. Learning from Imbalanced Classes
  7. 基于CNN的性别、年龄识别及Demo实现
  8. android listview和simpleadapter 给itme 中的控件添加事件
  9. django-restframework使用
  10. 验证Oracle收集统计信息参数granularity数据分析的力度