You are viewing a single comment's thread. Return to all comments →
anagram
static boolean isAnagram(String a, String b) { java.util.Map<Character, Integer> m = new java.util.HashMap(); for(int i = 0; i< a.length(); i++) { char x = Character.toLowerCase(a.charAt(i)); if(m.containsKey(x)) { m.put(x, m.get(x) + 1); } else { m.put(x, 1); } } for(int i = 0; i< b.length(); i++) { char x = Character.toLowerCase(b.charAt(i)); if(m.containsKey(x)) { int y = m.get(x); if(y > 0) m.put(x, y - 1); else return false; } else { return false; } } int sum = 0; for (java.util.Map.Entry<Character, Integer> entry : m.entrySet()) { sum += entry.getValue(); } // above loop can be replaced with: //if ( a.length() != b.length() ) // return false; return sum == 0 ? true : false ; }
Seems like cookies are disabled on this browser, please enable them to open this website
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
anagram