1100. Find K-Length Substrings With No Repeated Characters
Description
Given a string s
and an integer k
, return the number of substrings in s
of length k
with no repeated characters.
Example 1:
1 2 3
| Input: s = "havefunonleetcode", k = 5 Output: 6 Explanation: There are 6 substrings they are: 'havef','avefu','vefun','efuno','etcod','tcode'.
|
Example 2:
1 2 3
| Input: s = "home", k = 5 Output: 0 Explanation: Notice k can be larger than the length of s. In this case, it is not possible to find any substring.
|
Constraints:
1 <= s.length <= 10^4
s
consists of lowercase English letters.
1 <= k <= 10^4
Hints/Notes
- 2024/10/20
- sliding window
- premium
Solution
Language: C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public: int numKLenSubstrNoRepeats(string s, int k) { int n = s.size(), right = 0, res = 0, valid = 0; vector<int> m(26, 0); while (right < n) { char c = s[right]; m[c - 'a']++; if (m[c - 'a'] == 1) { valid++; } if (right >= k - 1) { if (valid == k) { res++; } char l = s[right - k + 1]; m[l - 'a']--; if (m[l - 'a'] == 0) { valid--; } } right++; } return res; } };
|