[LeetCode] Find K-Length Substrings With No Repeated Characters

1100. Find K-Length Substrings With No Repeated Characters

Given a string s and an integer k, return the number of substrings in s of length k with no repeated characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int numKLenSubstrNoRepeats(string s, int k) {
if(k > 26 or s.length() < k) return 0;
int arr[26]{0,}, l = 0, r = 0, n = s.length(), res = 0;

while(r < n) {
arr[s[r++]-'a']++;
while(arr[s[r-1]-'a'] > 1 or l + k < r) {
arr[s[l++]-'a']--;
}
if(l + k == r){
res++;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/15/PS/LeetCode/find-k-length-substrings-with-no-repeated-characters/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.