Java solution #HashMap #Unicode #Follow-up


  • 2

    Here is a typical solution using a hash table without considering Unicode support.

        public boolean isAnagram(String s, String t) {
            if (s==null && t==null) return true;
            else if (s==null || t==null) return false;
            else if (s.length() != t.length()) return false;
            
            Map<Character, Integer> dict = new HashMap<>();
            for(char c : s.toCharArray()) dict.put(c, dict.getOrDefault(c, 0) + 1);
            for(char c : t.toCharArray()) {
                int count = dict.getOrDefault(c, 0);
                if (count == 0) return false;
                else dict.put(c, count - 1);
            }
            
            return true;
        }
    

    In Java, a Unicode could be represented by a single char(BMP, Basic Multilingual Plane) or two chars (high surrogate). Bascially, we can use

    • String.codePointAt(int index) method to get the integer representation of a Unicode (as the key in the hash table)
    • and use Character.charCount(int code) to count how many the characters are used there (to correctly increase our index)
    public class Solution {
        public boolean isAnagram(String s, String t) {
            if (s==null && t==null) return true;
            else if (s==null || t==null) return false;
            else if (s.length() != t.length()) return false;
            
            Map<Integer, Integer> dict = new HashMap<>();
            int index = 0;
            while(index < s.length()) {
                int charCode = s.codePointAt(index); // Get the integer representation of Unicode 
                dict.put(charCode, dict.getOrDefault(charCode, 0) + 1);
                index += Character.charCount(charCode); // The Unicode could be represented by either one char or two chars
            }
            
            index = 0;
            while(index < t.length()) {
                int charCode = t.codePointAt(index);
                int count = dict.getOrDefault(charCode, 0);
                
                if (count == 0) return false;
                else dict.put(charCode, count - 1);
                
                index += Character.charCount(charCode);
            }
            
            return true;
        }
    }
    

    In Java 8, we can use Charsequence.codepoints() to simplify our code.

    public class Solution {
        public boolean isAnagram(String s, String t) {
            if (s==null && t==null) return true;
            else if (s==null || t==null) return false;
            else if (s.length() != t.length()) return false;
    
            final Map<Integer, Integer> dict = new HashMap<>();
            s.codePoints().forEach(code -> dict.put(code, dict.getOrDefault(code, 0) + 1));
            t.codePoints().forEach(code -> dict.put(code, dict.getOrDefault(code, 0) - 1));
            
            for(int count : dict.values()) {
                if (count<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.