-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path242. Valid Anagram.go
60 lines (55 loc) · 934 Bytes
/
242. Valid Anagram.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
// Runtime0 ms
// Beats
// 100%
// Memory2.8 MB
// Beats
// 82.24%
package main
import (
"fmt"
)
func main() {
s := "anagrm"
r := "nagaram"
fmt.Println(isAnagram(s, r))
}
// func isAnagram(s string, t string) bool {
// arr1 := []int{}
// for i := 0; i < 58; i++ {
// arr1 = append(arr1, 0)
// }
// arr2 := []int{}
// for i := 0; i < 58; i++ {
// arr2 = append(arr2, 0)
// }
// for i := 0; i < len(s); i++ {
// arr1[s[i]-'A']++
// }
// for i := 0; i < len(t); i++ {
// arr2[t[i]-'A']++
// }
// ans := true
// for i := 0; i < 58; i++ {
// if arr1[i] != arr2[i] {
// ans = false
// return ans
// }
// }
// return ans
// }
func isAnagram(s string, t string) bool {
var arr1 [95]int
var arr2 [95]int
for i := 0; i < len(s); i++ {
arr1[s[i]-' ']++
}
for i := 0; i < len(t); i++ {
arr2[t[i]-' ']++
}
for i := 0; i < 95; i++ {
if arr1[i] != arr2[i] {
return false
}
}
return true
}