-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution1941.go
66 lines (60 loc) · 1.32 KB
/
solution1941.go
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
package solution1941
// ============================================================================
// 1941. Check if All Characters Have Equal Number of Occurrences
// URL: https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/1941---Check-if-All-Characters-Have-Equal-Number-of-Occurrences
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_areOccurrencesEqual-24 63904807 17.44 ns/op 0 B/op 0 allocs/op
Benchmark_areOccurrencesEqual_map-24 4245451 281.9 ns/op 752 B/op 3 allocs/op
PASS
*/
func areOccurrencesEqual(s string) bool {
m := [26]int{}
for _, char := range s {
ch := byte(char) - 'a'
m[ch]++
}
c := -1
i := 0
for _, v := range m {
if v != 0 {
if c == -1 {
c = v
}
if c != v {
return false
}
}
i++
}
return true
}
func areOccurrencesEqual_map(s string) bool {
m := make(map[byte]int, 26)
for _, char := range s {
ch := byte(char)
_, ok := m[ch]
if !ok {
m[ch] = 1
} else {
m[ch]++
}
}
c := -1
i := 0
for _, v := range m {
if i == 0 {
c = v
}
if c != v {
return false
}
i++
}
return true
}