My solution based on count sort


  • 0
    H

    import java.util.HashMap;

    public class Solution {
    public boolean isAnagram(String s, String t) {

        if(s.length()!=t.length()) return false;
        
        if(s.length()==0&&t.length()==0) return true;
        
        HashMap<Character, Integer> myMap = new HashMap<Character, Integer>();
        
        for(int i=0;i<s.length();i++)
        {
            if(!myMap.containsKey(s.charAt(i)))
            {
                myMap.put(s.charAt(i),1);
            }
            else
            {
                Integer temp = myMap.get(s.charAt(i));
                myMap.put(s.charAt(i),temp+1);
            }
            
        }
        
        for(int i=0;i<t.length();i++)
        {
            if(myMap.containsKey(t.charAt(i)))
            {
                Integer temp = myMap.get(t.charAt(i));
                myMap.put(t.charAt(i),temp-1);
                
                if(temp==1)
                {
                    myMap.remove(t.charAt(i));
                }
            }
            
            
        }
        
        if(myMap.isEmpty())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    

    }


Log in to reply
 

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