Beat 98.3% 3ms java solution


  • 2
    Z
    public class Solution 
    {
        public boolean isAnagram(String s, String t) 
    	{
            if (s.length() != t.length())
    			return false;
    		
    		char[] sChar = s.toCharArray();
    		char[] tChar = t.toCharArray();
    		int[] temp = new int[128];
    		
    		for (char c : sChar)
    		{
    			temp[c]++;
    		}
    		for (char c : tChar)
    		{
    			temp[c]--;
    			
    			if (temp[c] < 0)
    				return false;
    		}
    		
    		return true;
        }
    }

  • 1
    C

    I haven't tried your answer but I think it's fast.Is the number 128 the weakness when we expend this solution to Unicode?


  • 0
    Z

    yes, expend to unicode will cover more cases and definitely is better. At first i just wanna share the mapping point so did not care too much about that, but if interview i will ask for the range~


  • 0
    E

    How you guys think about dealing with Unicode? By a HashMap with same idea?


Log in to reply
 

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