Skip to content

Commit 8ed8d3a

Browse files
committed
refactor Valid Anagram
1 parent ae64361 commit 8ed8d3a

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

go/valid_anagram.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,31 @@
22
package main
33

44
func isAnagram(s string, t string) bool {
5-
if len(s) != len(t) {
6-
return false
5+
var frequency [26]int
6+
for _, r := range s {
7+
frequency[r-'a']++
78
}
8-
9-
var freq [26]int
10-
for i := 0; i < len(s); i++ {
11-
freq[s[i]-'a']++
12-
freq[t[i]-'a']--
9+
for _, r := range t {
10+
frequency[r-'a']--
11+
}
12+
for _, n := range frequency {
13+
if n != 0 {
14+
return false
15+
}
1316
}
17+
return true
18+
}
1419

15-
for _, v := range freq {
16-
if v != 0 {
20+
func isAnagram_unicode(s string, t string) bool {
21+
frequency := make(map[rune]int)
22+
for _, r := range s {
23+
frequency[r]++
24+
}
25+
for _, r := range t {
26+
frequency[r]--
27+
}
28+
for _, n := range frequency {
29+
if n != 0 {
1730
return false
1831
}
1932
}

go/valid_anagram_another_solution.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)