generic Java Solution irrespective of alpha case or special characters - Feedback appreciated


  • 0
    public boolean isAnagram(String s, String t) {
            if (s.length() != t.length()) {
                return false;
            }
            Map<Character, Integer> m = new HashMap<>();
            for (Character c : s.toCharArray()) {
                int count = m.containsKey(c) ? m.get(c) : 0;
                m.put(c, ++count);
            }
            for (Character c : t.toCharArray()) {
                int count = m.containsKey(c) ? m.get(c) : 0;
                if (count - 1 == 0) {
                    m.remove(c);
                } else {
                    m.put(c, --count);
                }
            }
            return m.size() == 0;
        }
    

Log in to reply
 

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