Skip to content

Instantly share code, notes, and snippets.

@samocodes
Created May 11, 2024 02:50
Show Gist options
  • Save samocodes/dd770c8e0a40b061e32cfa795c015b78 to your computer and use it in GitHub Desktop.
Save samocodes/dd770c8e0a40b061e32cfa795c015b78 to your computer and use it in GitHub Desktop.
func isAnagram(s string, t string) bool {
char := make([]int, 26)
for _, v := range s {
i := int(v - 'a')
char[i]++
}
for _, v := range t {
i := int(v - 'a')
char[i]--
}
for _, v := range char {
if v != 0 {
return false
}
}
return true
}
@samocodes
Copy link
Author

Solution 2

func isAnagram(s string, t string) bool {
    n := len(s)
    
    if n != len(t) { return false }

    var char [26]int

    for i := 0; i < n; i++ {
        char[s[i] - 'a']++
        char[t[i] - 'a']--
    }
    
    for _, v := range char {
        if v != 0 {
            return false
        }
    }

    return true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment