A 3ms simple solution with golang.


  • 0
    X
    func isAnagram(s string, t string) bool {
        if len(s) != len(t) {
            return false
        }
        record := make([]int, 26)
        count := len(s)
        // Just to store the occurences of a character
        for _, v := range s {
            record[v-'a']++
        }
        for _, v := range t {
            // Minus the count when meeting a same character in s
            if record[v-'a'] >= 1 {
                count--
            }
            record[v-'a']--
        }
        // count should be zero if all the character in t also in s
        return count == 0
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.