blob: 49b6fafe136a2bb01e74f6056c423cca9e9efc04 (
plain) (
blame)
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
|
package main
func isAnagram_v_1(s string, t string) bool {
s_map := make(map[rune]int, len(s))
t_map := make(map[rune]int, len(t))
for _, character := range s {
s_map[character]++
}
for _, character := range t {
t_map[character]++
}
if len(s_map) != len(t_map) {
return false
}
for k, v := range s_map {
value, ok := t_map[k]
if !ok {
return false
}
if value != v {
return false
}
}
return true
}
func isAnagram(s string, t string) bool {
s_map := make([]int, 26)
t_map := make([]int, 26)
for _, character := range s {
s_map[int(character-'a')]++
}
for _, character := range t {
t_map[int(character-'a')]++
}
if len(s_map) != len(t_map) {
return false
}
for i := 0; i < 26; i++ {
if s_map[i] != t_map[i] {
return false
}
}
return true
}
func main() {
isAnagram("anagram", "nagaram")
}
|