Java Anagrams

  • + 0 comments
    static boolean isAnagram(String a, String b) {
        // Complete the function
        java.util.Map<Character, Integer> stringAMap = new java.util.HashMap<>();
        java.util.Map<Character, Integer> stringBMap = new java.util.HashMap<>();
    
        if(a.length() == b.length()) {
            for(int i = 0; i < a.length(); i++){
                stringAMap.compute(a.toLowerCase().charAt(i), (key, value) -> value == null ? 1 : value + 1);    
                stringBMap.compute(b.toLowerCase().charAt(i), (key, value) -> value == null ? 1 : value + 1);    
            }
            if(stringAMap.equals(stringBMap)){
                return true;
            }
            return false;
    
        }
        return false;
    }