What's wrong with my code??


  • 0
    L
    public class Solution {
        public boolean isAnagram(String s, String t) {
            if(s==null&&t==null||s.equals(t))
    			return true;
    		else
    		{
    			if(s==null&&t!=null||s!=null&&t==null)
    				return false;
    			else
    			{
    				HashMap<Character, Integer> sHm = new HashMap<>();
    				HashMap<Character, Integer> tHm = new HashMap<>();
    				for(int i = 0; i < s.length(); i++)
    				{
    					if(!sHm.containsKey(s.charAt(i)))
    					{
    						sHm.put(s.charAt(i), 1);
    					}
    					else
    					{
    						sHm.put(s.charAt(i), sHm.get(s.charAt(i))+1);
    					}
    				}
    				for(int i = 0; i < t.length(); i++)
    				{
    					if(!tHm.containsKey(t.charAt(i)))
    					{
    						tHm.put(t.charAt(i), 1);
    					}
    					else
    					{
    						tHm.put(t.charAt(i), tHm.get(t.charAt(i))+1);
    					}
    				}
    				if(!sHm.keySet().containsAll(tHm.keySet())||!tHm.keySet().containsAll(sHm.keySet()))
    				{
    					return false;
    				}
    				else
    				{
    					for(char key : sHm.keySet())
    					{
    						if(sHm.get(key)!=tHm.get(key))
    							return false;
    					}
    					return true;
    				}
    			}
    		}
        }
    }
    

    Anyone help?


Log in to reply
 

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