6-line 120ms C# Space O(1) Time O(n) Solution

  • 0
    public bool IsAnagram(string s, string t) {
        if(s.Length != t.Length) return false;
        int[] dict = new int[26];
        for(int i = 0; i < s.Length; i++)
        { dict[s[i] - 'a']++; dict[t[i] - 'a']--; }
        foreach(var d in dict) if(d != 0) return false;
        return true;

Log in to reply

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