My Java solution


  • 0
    M

    class Solution {
        public boolean isAnagram(String s, String t) {
            Map<Character, Integer> mp = new HashMap<Character, Integer>();
    		if (s.length() != t.length()) return false;
    		for (char c : s.toCharArray()) {
    			Integer freq = mp.get(c);
    			mp.put(c, freq == null ? 1 : freq + 1);
    		}
    		for (char c : t.toCharArray()) {
    			Integer freq = mp.get(c);
    			if (freq == null || freq == 0) return false;
    			mp.put(c, mp.get(c) - 1);
    		}
    		for (Integer value : mp.values()) {
    			if (value != 0) return false;
    		}
    		return true;
        }
    }
    

Log in to reply
 

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