My solution using hashtable in C#


  • 0
    Z

    public class Solution {
    public bool IsAnagram(string s, string t) {
    if (s.Length != t.Length)
    {
    return false;
    }
    Dictionary<char, int> dic = new Dictionary<char, int>();

            for(int i = 0; i < s.Length; i++)
            {
                if (dic.ContainsKey(s[i]))
                {
                    dic[s[i]]++;
                }
                else
                {
                    dic.Add(s[i], 1);
                }
            }
            for(int i = 0; i < t.Length; i++)
            {
                if (!dic.ContainsKey(t[i]))
                    return false;
                else
                {
                    if (dic[t[i]] > 1)
                        dic[t[i]]--;
                    else
                        dic.Remove(t[i]);
                }
            }
            return dic.Count > 0 ? false : true;
    }
    

    }


Log in to reply
 

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