Accepted C# Solution


  • 0
    M
    public bool IsAnagram(string s, string t) {
                    if (s == null && t == null) return true;
            if (s == null || t == null) return false; 
    
            if (s.Length != t.Length) return false;
    
            var num = 0;
            var table = new int[256];
            foreach (var c in s)
            {
                if (table[c] == 0) num++;
                table[c]++;
            }
    
            foreach (var c in t)
            {
                table[c]--;
                if (table[c] == 0) num--;
                if (table[c] < 0) return false;
            }
            return num == 0;
    }

Log in to reply
 

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