LeetCode 1100. Find K-Length Substrings With No Repeated Characters
原题链接在这里: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 <= S.length <= 10^4- All characters of S are lowercase English letters.
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的更多相关文章
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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 ...
- LeetCode:前K个高频单词【692】
LeetCode:前K个高频单词[692] 题目描述 给一非空的单词列表,返回前 k 个出现次数最多的单词. 返回的答案应该按单词出现频率由高到低排序.如果不同的单词有相同出现频率,按字母顺序排序. ...
- LeetCode:前K个高频元素【347】
LeetCode:前K个高频元素[347] 题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [ ...
- LeetCode:第K个排列【60】
LeetCode:第K个排列[60] 题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: &quo ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
随机推荐
- Unary模式下客户端创建 default-executor 和 resolver-executor 线程和从启动到执行grpc_connector_connect的主要流程
(原创)C/C/1.25.0-dev grpc-c/8.0.0, 使用的例子是自带的例子GreeterClient 创建 default-executor 和 resolver-executor 线程 ...
- vue的package.json文件理解
参考文档: https://www.cnblogs.com/tzyy/p/5193811.html#_h1_0 https://www.cnblogs.com/hongdiandian/p/83210 ...
- SocketChannel简述
前言 在前面的Channel概述的分类中提到过SocketChannel主要是用来基于TCP通信的通道.这篇文章详细介绍下SocketChannel SocketChannel是什么 SocketCh ...
- 可落地的DDD(3)-如何利用DDD进行微服务的划分
摘要 前面两篇介绍了DDD的目标管理.DDD的工程结构调整.这篇讨论微服务的划分.微服务是目前后端比较流行的架构体系了,那么如何做好一个微服务的划分?一个微服务的粒度应该是多大呢?这篇主要介绍如何结合 ...
- asp.net core 系列之Performance的 Response compression(响应压缩)
本文,帮助了解响应压缩的一些知识及用法(大部分翻译于官网,英文水平有限,不准确之处,欢迎指正). 什么是响应压缩?响应压缩简单的说就是为了减少网络带宽,而把返回的响应压缩,使之体积缩小,从而加快响应的 ...
- Android 8.0的平台上,应用不能对大部分的广播进行静态注册
引言在Android 8.0的平台上,应用不能对大部分的广播进行静态注册,也就是说,不能在AndroidManifest文件对有些广播进行静态注册,这里必须强调是有些广播,因为有些广播还是能够注册的. ...
- 【JVM】CMS垃圾回收器
一.简介 Concurrent Mark Sweep,是一种以获取最短回收停顿时间为目标的收集器,尤其重视服务的响应速度. CMS是老年代垃圾回收器,基于标记-清除算法实现.新生代默认使用ParNew ...
- hello world之vivado程序解决方法
体验米尔zynq系列Z-turn Board单板时,我开始用vivado.在安装vivad工程中出了一些问题,经过不懈的重新安装,终于成功了. 下面分享我用vivado设计hello world程序: ...
- Electron学习入门
1.安装electron,不建议全局安装,这样每个app可以使用不同的electron版本了 2.配置package.json中的script下的start属性的值为electron . Electr ...
- 英语hawkbillturtle玳瑁
玳瑁(hawkbillturtle):属爬行纲,海龟科的海洋动物.一般长约0.6米,大者可达1.6米.头顶有两对前额鳞,吻部侧扁,上颚前端钩曲呈鹰嘴状:前额鳞2对:背甲盾片呈覆瓦状排列:背面的角质板覆 ...