Skip to content

Commit 13be050

Browse files
committed
add step3 for Valid Anagram
1 parent 4f14395 commit 13be050

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pullrequests/valid_anagram/step3.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//lint:file-ignore U1000 Ignore all unused code
2+
package validanagram
3+
4+
func isAnagram_step3(s string, t string) bool {
5+
return frequency(s) == frequency(t)
6+
}
7+
8+
func frequency(s string) [26]int {
9+
var f [26]int
10+
for _, r := range s {
11+
f[r-'a']++
12+
}
13+
return f
14+
}
15+
16+
// ちゃんとUnicodeに対応させるなら結合文字などを考慮する必要がある
17+
// https://github.com/rihib/leetcode/pull/5#discussion_r1706198268
18+
func isAnagram_unicode_step3(s string, t string) bool {
19+
frequency := make(map[rune]int)
20+
for _, r := range s {
21+
frequency[r]++
22+
}
23+
for _, r := range t {
24+
frequency[r]--
25+
}
26+
for _, n := range frequency {
27+
if n != 0 {
28+
return false
29+
}
30+
}
31+
return true
32+
}

0 commit comments

Comments
 (0)