Fails last test case, please help


  • 0
    M
    public class Solution {
        public boolean isAnagram(String s, String t) {
            if(s.length() != t.length()) return false;
            
            HashMap<Character, Integer> sMap = new HashMap<Character, Integer>();
            HashMap<Character, Integer> tMap = new HashMap<Character, Integer>();
            
            //itereate thru first and add to hashmap
            for(char c : s.toCharArray()) {
                if(sMap.containsKey(c)) sMap.put(c, sMap.get(c) + 1);
                else sMap.put(c, 1);
            }
            
            //iterate thru second and add to hashmap
            for(char c : t.toCharArray()) {
                if(tMap.containsKey(c)) tMap.put(c, tMap.get(c) + 1);
                else tMap.put(c, 1);
            }
            
            // go through map and make sure all keys have the same value
            for(char c : sMap.keySet()) {
                if(sMap.get(c) != tMap.get(c)) return false;
            }
    
            
            return true;
        }
    }
    
    

Log in to reply
 

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