Skip to content

Commit

Permalink
add: #242 valid anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
ysle0 committed Dec 18, 2024
1 parent 4666f02 commit 3f1552f
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions valid-anagram/ysle0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

// package valid_anagram

import (
"fmt"
)

func isAnagram(s string, t string) bool {
ht := make(map[rune]int, len(s))
for _, r := range s {
ht[r]++
}

for _, r := range t {
cnt, ok := ht[r]
if !ok {
return false
}

ht[r] -= 1
if cnt-1 < 0 {
return false
}
}

for _, v := range ht {
if v > 0 {
return false
}
}

return true
}

func main() {
r1 := isAnagram("anagram", "nagaram")
fmt.Println(r1)
fmt.Println()

r2 := isAnagram("rat", "car")
fmt.Println(r2)

r3 := isAnagram("ab", "a")
fmt.Println(r3)
}

0 comments on commit 3f1552f

Please sign in to comment.