C# solution: dictionary


  • 0
    B
    public class Solution 
    {
        public bool IsAnagram(string s, string t) 
    	{
    		if (s == "" && t == "") return true;
    		if (s == "" || t == "") return false;
    
    		if (s.Length != t.Length)	return false;
    
            var charAndCount = new int[256];
    
    		foreach(var c in s)
    		{
    			charAndCount[c]++;
    		}
    
    		foreach(var c in t)
    		{
    			charAndCount[c]--;
    
    			if (charAndCount[c] < 0) return false;
    		}
    
    		var result = charAndCount.Any(c => c != 0);
    
    		return !result;
        }
    }
    

Log in to reply
 

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