Skip to content

Commit

Permalink
string/valid-anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
aolenevme authored Nov 22, 2021
1 parent 6c38ebe commit 6fa8ba2
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions string/valid-anagram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 104
s and t consist of lowercase English letters.
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
**/

package main

import (
"fmt"
)

func main() {
fmt.Println(isAnagram("anagram", "nagaram"))
fmt.Println(isAnagram("rat", "car"))
}

func isAnagram(s string, t string) bool {
kv := make(map[rune]int)

for _, char := range s {
kv[char]++
}

for _, char := range t {
kv[char]--
}

res := true
for _, v := range kv {
if v != 0 {
res = false
}
}

return res
}

0 comments on commit 6fa8ba2

Please sign in to comment.