-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3662-FilterCharactersByFrequency.go
More file actions
91 lines (78 loc) · 2.71 KB
/
3662-FilterCharactersByFrequency.go
File metadata and controls
91 lines (78 loc) · 2.71 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
// 3662. Filter Characters by Frequency
// You are given a string s consisting of lowercase English letters and an integer k.
// Your task is to construct a new string that contains only those characters from s which appear fewer than k times in the entire string.
// The order of characters in the new string must be the same as their order in s.
// Return the resulting string. If no characters qualify, return an empty string.
// Note: Every occurrence of a character that occurs fewer than k times is kept.
// Example 1:
// Input: s = "aadbbcccca", k = 3
// Output: "dbb"
// Explanation:
// Character frequencies in s:
// 'a' appears 3 times
// 'd' appears 1 time
// 'b' appears 2 times
// 'c' appears 4 times
// Only 'd' and 'b' appear fewer than 3 times. Preserving their order, the result is "dbb".
// Example 2:
// Input: s = "xyz", k = 2
// Output: "xyz"
// Explanation:
// All characters ('x', 'y', 'z') appear exactly once, which is fewer than 2. Thus the whole string is returned.
// Constraints:
// 1 <= s.length <= 100
// s consists of lowercase English letters.
// 1 <= k <= s.length
import "fmt"
func filterCharacters(s string, k int) string {
res, mp := make([]byte, 0), make(map[byte]int)
for i := 0; i < len(s); i++ {
mp[s[i]]++
}
for i := 0; i < len(s); i++ {
if mp[s[i]] >= k {
continue
}
res = append(res, s[i])
}
return string(res)
}
func filterCharacters1(s string, k int) string {
freq := map[byte]int{}
for i := 0; i < len(s); i++ {
freq[s[i]]++
}
res := []byte{}
for i := 0; i < len(s); i++ {
if freq[s[i]] < k {
res = append(res, s[i])
}
}
return string(res)
}
func main() {
// Example 1:
// Input: s = "aadbbcccca", k = 3
// Output: "dbb"
// Explanation:
// Character frequencies in s:
// 'a' appears 3 times
// 'd' appears 1 time
// 'b' appears 2 times
// 'c' appears 4 times
// Only 'd' and 'b' appear fewer than 3 times. Preserving their order, the result is "dbb".
fmt.Println(filterCharacters("aadbbcccca", 3)) // dbb
// Example 2:
// Input: s = "xyz", k = 2
// Output: "xyz"
// Explanation:
// All characters ('x', 'y', 'z') appear exactly once, which is fewer than 2. Thus the whole string is returned.
fmt.Println(filterCharacters("xyz", 2)) // xyz
fmt.Println(filterCharacters("bluefrog", 2)) // bluefrog
fmt.Println(filterCharacters("leetcode", 2)) // ltcod
fmt.Println(filterCharacters1("aadbbcccca", 3)) // dbb
fmt.Println(filterCharacters1("xyz", 2)) // xyz
fmt.Println(filterCharacters1("bluefrog", 2)) // bluefrog
fmt.Println(filterCharacters1("leetcode", 2)) // ltcod
}