原题链接在这里: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:

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

Example 2:

  1. Input: S = "home", K = 5
  2. Output: 0
  3. Explanation:
  4. 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:

  1. class Solution {
  2. public int numKLenSubstrNoRepeats(String S, int K) {
  3. if(S == null || S.length() < K){
  4. return 0;
  5. }
  6.  
  7. int [] map = new int[26];
  8. int runner = 0;
  9. int count = 0;
  10. int res = 0;
  11. while(runner < S.length()){
  12. if(map[S.charAt(runner)-'a']++ > 0){
  13. count++;
  14. }
  15.  
  16. if(runner >= K){
  17. if(map[S.charAt(runner-K)-'a']-- > 1){
  18. count--;
  19. }
  20. }
  21.  
  22. if(runner >=K-1 && count == 0){
  23. res++;
  24. }
  25.  
  26. runner++;
  27. }
  28.  
  29. return res;
  30. }
  31. }

类似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. Effective.Java第12-22条

    12.  始终重写toString()方法 如果不重写toString()方法,打印的时候是 类名+@+哈希码的无符号十六进制.我们查看 Object的toString()方法如下: public S ...

  2. ipv4的ip字符串转化为int型

    要求: 将现有一个ipv4的ip字符串(仅包含数字,点,空格), 其中数字和点之间的空格(至多一个)是合法的,比如“12 .3. 4 .62”,其他情况均为非法地址.写一个函数将ipv4地址字符串转化 ...

  3. java 操作实例

    1.无重复字符的最长子串 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. class Soluti ...

  4. 10. Scala数据结构(上)-集合操作

    10.1 数据结构特点 10.1.1 Scala集合基本介绍 uml => 统一建模语言 1) Scala同时支持不可变集合和可变集合,不可变集合可以安全的并发访问 两个主要的包 不可变集合:s ...

  5. 移相器——K波段有源移相器设计

    博主之前在做一款K波段有源移相器,所用工艺为smic55nmll工艺,完成了几个主要模块的仿真,现对之前的工作做个总结. K波段的频率范围是18G——27GHz,所设计移相器的工作频率范围是19G—— ...

  6. GoldenDict(for Linux)配置无道词典

    引言 我原来写过一篇博客:(离线)英语词典软件推荐,个人比较喜欢的就是GoldenDict词典.不仅仅是因为它是免费开源的多平台程序,更重要的是支持丰富的原版词典(下文给出了下载链接).本文主要针对其 ...

  7. SQL Server返回DATETIME类型,年、月、日、时、分、秒、毫秒

    SQL Server返回DATETIME类型的年.月.日,有两种方法,如下所示: DECLARE @now DATETIME=GETDATE() --第一种方法 SELECT @now,YEAR(@n ...

  8. Redis高级功能-1、高并发基本概述

    1.可能的问题 要将redis运用到工程项目中,只使用一台redis是万万不能的,原因如下: (1)从结构上,单个redis服务器会发生单点故障,并且一台服务器需要处理所有的请求负载,压力较大. (2 ...

  9. 使用EF Core+CodeFirst建立ASP.NET Core MVC项目

    本篇随笔介绍如何使用.NET Core+EF Core创建Web应用程序 首先借用官网的话简单介绍一下ASP.NET Core ASP.NET Core 是一个跨平台的高性能开源框架,用于生成基于云且 ...

  10. JavaScript 获取页面元素

    一.根据 id 获取元素 语法格式: document.getElementById(id); Demo: var main = document.getElementById('main'); co ...