-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3329-CountSubstringsWithKFrequencyCharactersII.go
More file actions
61 lines (54 loc) · 1.63 KB
/
3329-CountSubstringsWithKFrequencyCharactersII.go
File metadata and controls
61 lines (54 loc) · 1.63 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
// 3329. Count Substrings With K-Frequency Characters II
// Given a string s and an integer k,
// return the total number of substrings of s where at least one character appears at least k times.
// Example 1:
// Input: s = "abacb", k = 2
// Output: 4
// Explanation:
// The valid substrings are:
// "aba" (character 'a' appears 2 times).
// "abac" (character 'a' appears 2 times).
// "abacb" (character 'a' appears 2 times).
// "bacb" (character 'b' appears 2 times).
// Example 2:
// Input: s = "abcde", k = 1
// Output: 15
// Explanation:
// All substrings are valid because every character appears at least once.
// Constraints:
// 1 <= s.length <= 3 * 10^5
// 1 <= k <= s.length
// s consists only of lowercase English letters.
import "fmt"
func numberOfSubstrings(s string, k int) int64 {
res, i := int64(0), 0
count := [26]int{}
for _, v := range s {
count[v - 'a']++
for count[v - 'a'] >= k {
count[s[i] - 'a']--
i++
}
res += int64(i)
}
return res
}
func main() {
// Example 1:
// Input: s = "abacb", k = 2
// Output: 4
// Explanation:
// The valid substrings are:
// "aba" (character 'a' appears 2 times).
// "abac" (character 'a' appears 2 times).
// "abacb" (character 'a' appears 2 times).
// "bacb" (character 'b' appears 2 times).
fmt.Println(numberOfSubstrings("abacb", 2)) // 4
// Example 2:
// Input: s = "abcde", k = 1
// Output: 15
// Explanation:
// All substrings are valid because every character appears at least once.
fmt.Println(numberOfSubstrings("abcde", 1)) // 15
}