import java.util.HashMap;

/**
 * Problem: #242 Valid Anagram
 *
 * Idea: count the character and corresponding number of times, compare the result
 *
 * Solutions:
 *  isAnagram: firstly count the letters and appearing times and save them to hashmaps,
 * compare if the two maps have the same entry set.
 *
 * Note: #canbefaster
 *
 */
public class ValidAnagram {
    public boolean haveSameLength(String s, String t) {
        return s.length() == t.length();
    }

    public HashMap<Character, Integer> hashString(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 0);
            }
        }
        return map;
    }

    public boolean isAnagram(String s, String t) {
        if (haveSameLength(s, t)) {
            if (s.isEmpty()) return true;

            HashMap<Character, Integer> maps = hashString(s);

            HashMap<Character, Integer> mapt = hashString(t);

            return maps.entrySet().equals(mapt.entrySet());
        }
        return false;
    }

    public static void main(String[] args) {
        ValidAnagram va = new ValidAnagram();
        System.out.println(va.isAnagram("", ""));
        System.out.println(va.isAnagram("anagram", "nagaram"));
        System.out.println(va.isAnagram("rat", "car"));
        System.out.println(va.isAnagram("a", "b"));
        System.out.println(va.isAnagram("a", "a"));
    }
}

@@ -1206,379 +1206,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        ValidAnagram va = new ValidAnagram();%0A        System.out.println(va.isAnagram(%22%22, %22%22));%0A        System.out.println(va.isAnagram(%22anagram%22, %22nagaram%22));%0A        System.out.println(va.isAnagram(%22rat%22, %22car%22));%0A        System.out.println(va.isAnagram(%22a%22, %22b%22));%0A        System.out.println(va.isAnagram(%22a%22, %22a%22));%0A    %7D
 %0A%7D%0A

Remove main