原题链接在这里:https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/

题目:

Given a string S, return the number of substrings of length K with no repeated characters.

Example 1:

Input: S = "havefunonleetcode", K = 5
Output: 6
Explanation:
There are 6 substrings they are : 'havef','avefu','vefun','efuno','etcod','tcode'.

Example 2:

Input: S = "home", K = 5
Output: 0
Explanation:
Notice K can be larger than the length of S. In this case is not possible to find any substring.

Note:

  1. 1 <= S.length <= 10^4
  2. All characters of S are lowercase English letters.
  3. 1 <= K <= 10^4

题解:

Ask for the number of Size K window having no repeated characters.

Have runner to point the char in S. When frequency of this char is already >0, which means it appears before, then have count of repeated characters plus 1.

If runner >= K, then decrement S.charAt(runner-K) frequency. If its frequency is > 1 before decrement, then count of repeated characters minus 1.

If runner >= K-1 and there is no repeated characters, then res++.

Time Complexity: O(n). n = S.length.

Space: O(1).

AC Java:

 class Solution {
public int numKLenSubstrNoRepeats(String S, int K) {
if(S == null || S.length() < K){
return 0;
} int [] map = new int[26];
int runner = 0;
int count = 0;
int res = 0;
while(runner < S.length()){
if(map[S.charAt(runner)-'a']++ > 0){
count++;
} if(runner >= K){
if(map[S.charAt(runner-K)-'a']-- > 1){
count--;
}
} if(runner >=K-1 && count == 0){
res++;
} runner++;
} return res;
}
}

类似Longest Substring Without Repeating Characters.

LeetCode 1100. Find K-Length Substrings With No Repeated Characters的更多相关文章

  1. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  2. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  3. [leetcode]692. Top K Frequent Words K个最常见单词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  4. LeetCode:前K个高频单词【692】

    LeetCode:前K个高频单词[692] 题目描述 给一非空的单词列表,返回前 k 个出现次数最多的单词. 返回的答案应该按单词出现频率由高到低排序.如果不同的单词有相同出现频率,按字母顺序排序. ...

  5. LeetCode:前K个高频元素【347】

    LeetCode:前K个高频元素[347] 题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [ ...

  6. LeetCode:第K个排列【60】

    LeetCode:第K个排列[60] 题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: &quo ...

  7. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  8. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  9. 【LeetCode】696. Count Binary Substrings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...

随机推荐

  1. Mysql 数据库 表中列的操作

    [1]Mysql数据库中表的列操作 Mysql中关于表中列的操作集语句: -- [1]增加一列 ) DEFAULT NULL COMMENT '目的码区号'; -- [2]增加一列,在dnis_are ...

  2. C语言输入带空格的字符串

    参考:https://blog.csdn.net/vincemar/article/details/78750435 因为: scanf("%s",str); 遇到空格就停止接收后 ...

  3. 百度前端技术学院-task1.3源代码

    因为其中有图片,所以就给有图片的位置加了边框和设置了大小,这样哪怕图片不显示也可以知道在哪里. <!DOCTYPE html> <html> <head> < ...

  4. Dubbo面试踩坑

    1.Dubbo支持哪些协议,每种协议的应用场景,优缺点? dubbo: 单一长连接和NIO异步通讯,适合大并发小数据量的服务调用,以及消费者远大于提供者.传输协议TCP,异步,Hessian序列化: ...

  5. (Manjaro)VirtualBox异常修复:RTR3InitEx failed with rc=-1912 (rc=-1912)

    引言 VirtualBox运行异常好几天,其中尝试一些操作都没有解决. 版本说明 系统版本:4.19.88-1-MANJARO Vbox镜像:kali-linux-2019.4-vbox-amd64. ...

  6. C语言----输入输出语句(基础篇二)

    今天整理一下自己的基础篇输入和输出的理解,自己没有研究系统输入和输出函数,以后有时间在去深究,之前在别人的博客里面看到这么一句话分享给大家,“学习就是一个不断抄袭,模仿,练习和创新的一个过程”. 使用 ...

  7. MTSC 2019 深圳站精彩议题第一波更新! | 七五折门票火热售票中

      MTSC(中国移动互联网测试开发大会)到今年已经成功举办了五届,这四年里,TesterHome社区一直秉持着务实.能落地.有深度.高质量.重分享的原则,从讲师邀请到内容筛选都严格把控,只为将最能提 ...

  8. Boleto 银行付款

    Boleto是由多家巴西银行共同支持的一种支付方式,在巴西占据绝对主导地位,客户可以到巴西任何一家银行.ATM机.caipiao网点或使用网上银行授权银行转账. 该支付渠道有如下特点:1. 一旦付款, ...

  9. 前端获取文件input框的美化操作

    前面我们说了一种利用input框和js的当时获取本地文件内容的情况-详细信息参考 2017年11月8日前端用js获取本地文件的内容 以上方式获取的按钮是系统默认的显示,有时候我们需要对按钮的外观进行美 ...

  10. ubuntu升级python版本(3.5 -> 3.6)

    #获取最新的python3.6,将其添加至当前apt库中,并自动导入公钥 $ sudo add-apt-repository ppa:jonathonf/python-3.6 $ sudo apt-g ...