Post

Leetcode - 242. Valid Anagram

Leetcode - 242. Valid Anagram

Hits

  • Given two strings s and t, return true if t is an anagram of s, and false otherwise.

    An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func isAnagram(s string, t string) bool {
    if len(s)!=len(t){
        return false
    }
    charCount := make(map[rune]int) //rune used to handle Unicode character correctly
    
    for _, char := range s{
        charCount[char]++
    }
    
    for _, char := range t{
        charCount[char]--
        if charCount[char]<0{
            return false
        }
    }
    return true
}

This post is licensed under CC BY 4.0 by the author.