@@ -0,0 +1,1584 @@
+import java.util.HashMap;%0A%0A/**%0A * Problem: #242 Valid Anagram%0A *%0A * Idea: count the character and corresponding number of times, compare the result%0A *%0A * Solutions:%0A *  isAnagram: firstly count the letters and appearing times and save them to hashmaps,%0A * compare if the two maps have the same entry set.%0A *%0A * Note: #canbefaster%0A *%0A */%0Apublic class ValidAnagram %7B%0A    public boolean haveSameLength(String s, String t) %7B%0A        return s.length() == t.length();%0A    %7D%0A%0A    public HashMap%3CCharacter, Integer%3E hashString(String s) %7B%0A        HashMap%3CCharacter, Integer%3E map = new HashMap%3C%3E();%0A        for (int i = 0; i %3C s.length(); i++) %7B%0A            char c = s.charAt(i);%0A            if (map.containsKey(c)) %7B%0A                map.put(c, map.get(c) + 1);%0A            %7D else %7B%0A                map.put(c, 0);%0A            %7D%0A        %7D%0A        return map;%0A    %7D%0A%0A    public boolean isAnagram(String s, String t) %7B%0A        if (haveSameLength(s, t)) %7B%0A            if (s.isEmpty()) return true;%0A%0A            HashMap%3CCharacter, Integer%3E maps = hashString(s);%0A%0A            HashMap%3CCharacter, Integer%3E mapt = hashString(t);%0A%0A            return maps.entrySet().equals(mapt.entrySet());%0A        %7D%0A        return false;%0A    %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

Add new solution