只能说还是太菜,抄的网上大神的做法:

idea:

  1. mask 的每一位代表该位字母够不够k次,够k次为0,不够为1
  2. 对于每一位将其视为起点,遍历至末尾,找到其最大满足子串T的下标max_idx,之后从max_idx+1开始遍历。

代码如下:

    public int longestSubstring(String s, int k) {
if (k == 1) return s.length();
int res = 0, i = 0, n = s.length();
while (i + k <= n){
int[] m = new int[26];
int mask = 0, max_idx = i;
for (int j = i; j < n; j ++){
int t = s.charAt(j) - 'a';
m[t]++;
if (m[t] < k) mask |= (1 << t);
else mask &= (~(1 << t));
if (mask == 0){
res = Math.max(res, j - i + 1);
max_idx = j;
}
}
i = max_idx + 1;
}
return res;
}

下面一种做法的思路是:把串的小于k的字符当做间隔,直到这个串的每一个字符都大于等于k为止。

(一开始有这个思路,没写出来- - )

    public int helper(String s, int k, int left, int right){
int len = right - left + 1;
if (len <= 0) return 0;
int i, j, maxlen = 0;
int[] m = new int[26];
for (i = left; i <= right; i++)
m[s.charAt(i) - 'a']++;
for(i = left, j = left; i <= right; i++){
if (m[s.charAt(i) - 'a'] < k){
maxlen = Math.max(maxlen, helper(s, k, j, i - 1));
j = i + 1;
}
}
if (j == left) return len;
else return Math.max(maxlen, helper(s, k, j, i - 1));
} public int longestSubstring(String s, int k) {
return helper(s, k, 0, s.length() - 1);
}

还有滑动窗口的做法,接下来更新。

leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)的更多相关文章

  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 C#

    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

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  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. Debatching(Splitting) XML Message in Orchestration using DefaultPipeline - BizTalk 2010

    Debatching(Splitting) XML Message in Orchestration using DefaultPipeline - BizTalk 2010   In this po ...

  2. Java字符串和容器

    String Java.lang.String是Java的字符串类. Srting是一个不可变对象,所有对String修改的操作都需要构造新的String实例. String可以由char数组或字符串 ...

  3. [转]Using Angular in Visual Studio Code

    本文转自:https://code.visualstudio.com/docs/nodejs/angular-tutorial Using Angular in Visual Studio Code ...

  4. SQL Server中锁与事务隔离级别

    SQL Server中的锁分为两类: 共享锁 排它锁 锁的兼容性:事务间锁的相互影响称为锁的兼容性. 锁模式 是否可以持有排它锁 是否可以持有共享锁 已持有排它锁 否 否 已持有共享锁 否 是 SQL ...

  5. sort、sorted高级排序-Python3.7 And 算法<七>

    1.sort(*, key=None, reverse=False) sort()接受两个参数,这两个参数只能通过关键字(关键字参数)传递. 参数key:带一个参数的函数(排序时,会依次传入列表的每一 ...

  6. c++: Does the new operator for dynamic allocation check for memory safety?

    Quesion: My question arises from one of my c++ exercises (from Programming Abstraction in C++, 2012 ...

  7. Spring全家桶–SpringBoot Rest API

    Spring Boot通过提供开箱即用的默认依赖或者转换来补充Spring REST支持.在Spring Boot中编写RESTful服务与SpringMVC没有什么不同.总而言之,基于Spring ...

  8. base64加密和解码原理和代码

    Base64编码,是我们程序开发中经常使用到的编码方法.它是一种基于用64个可打印字符来表示二进制数据的表示方法.它通常用作存储.传输一些二进制数据编码方法!也是MIME(多用途互联网邮件扩展,主要用 ...

  9. python练习(-)

    简单的爬虫示例: import urllib.request #python2.x版本为urllib2url = 'http://www.douban.com/'webPage=urllib.requ ...

  10. Keras 中 TimeDistributed 和 TimeDistributedDense 理解

    From the offical code: class TimeDistributed(Wrapper): """This wrapper applies a laye ...