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) 方法二:连续子串计算 日 ...
随机推荐
- cmake 配置
罗列一下cmake常用的命令.CMake支持大写.小写.混合大小写的命令. 1. 添加头文件目录INCLUDE_DIRECTORIES 语法:include_directories([AFTER|BE ...
- LaTeX 小试牛刀
跟大家分享一下正式第一次使用 LaTex 的经验,之前数学建模的时候一直想用,但没有找到合适的软件.前段时间,实验室老师让我帮忙套个 IEEE ACCESS 的模板. 尝试过 TexPad,的确 UI ...
- 【题解】Luogu P5327 [ZJOI2019]语言
原题传送门 看到这种树上统计点对个数的题一般是线段树合并,这题也不出意外 先对这棵树进行树剖,对于每次普及语言,在\(x,y\)两点的线段树上的\(x,y\)两位置打\(+1\)标记,在点\(fa[l ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Java学习:static 关键字概述
static 关键字概述 一旦用了static关键字,那么这样的内容不再属于对象自己.而是属于类的,所以凡是本类的对象,都共享同一份. 如果没有static关键字,那么必须首先创建对象,然后通过对象才 ...
- CRLF will be replaced by LF in XXX when git commit
转载自Git-warning: CRLF will be replaced by LF in XXX 今天,普通平凡的一天,平凡的使用 git add .,然后又出现一个之前没遇到的错误提示 . 真开 ...
- linux或者shell进入vi命令
vi的基本操作 a) 进入vi 在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi file 不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(comman ...
- azure跨域问题(访问azure存储账户数据,blob)
访问azure存储账户数据报错:405错误 解决方案 打开访问的存储账户--->CORS--->Blob服务 全部填写*就可以了,点击“保存”即可. iframe就可以展示blob中的pd ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile
更换当前jdk版本为项目所需jdk版本即可
- sklearn中的KMeans算法
1.聚类算法又叫做“无监督分类”,其目的是将数据划分成有意义或有用的组(或簇).这种划分可以基于我们的业务需求或建模需求来完成,也可以单纯地帮助我们探索数据的自然结构和分布. 2.KMeans算法将一 ...