-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLC3325.cpp
More file actions
35 lines (27 loc) · 976 Bytes
/
LC3325.cpp
File metadata and controls
35 lines (27 loc) · 976 Bytes
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
27
28
29
30
31
32
33
34
35
/*
Explanation
1. Sliding Window:
• The left and right pointers define the current window.
• The window is expanded by moving the right pointer and shrunk by moving the left pointer.
2. Character Frequency Map:
• A hash map (unordered_map) is used to track the frequency of characters in the current window.
Complexity
• Time Complexity: O(26 * n), where:
• 26 is the maximum number of distinct characters (for lowercase English letters).
• n is the length of the string.
• Space Complexity: O(26) for the frequency map.
*/
class Solution {
public:
int numberOfSubstrings(string s, int k) {
int n = s.length(), res = (n + 1) * n / 2;
unordered_map<char, int> count;
for (int left = 0, right = 0; right < n; right++) {
count[s[right]]++;
while (count[s[right]] >= k)
--count[s[left++]];
res -= right - left + 1;
}
return res;
}
};