方法一: public boolean isAnagram(String s, String t) { if (s == null && t == null) { return true; } if (s == null || t == null) { return false; } Map<Character, Integer> mapS = new HashMap<>(); Map<Character, Integer> mapT = new HashMap<>();
for (int i = 0; i < s.length(); i++) { if (mapS.containsKey(s.charAt(i))) { mapS.put(s.charAt(i), mapS.get(s.charAt(i)) + 1); } else { mapS.put(s.charAt(i), 1); } }
for (int i = 0; i < t.length(); i++) { if (mapT.containsKey(t.charAt(i))) { mapT.put(t.charAt(i), mapT.get(t.charAt(i)) + 1); } else { mapT.put(t.charAt(i), 1); } }
if (mapS.equals(mapT)) { return true; } return false; } 方法二: public boolean isAnagram(String s, String t) { if (s == null && t == null) { return true; } if (s == null || t == null) { return false; } if(s.length() != t.length()){ return false; } char[] ss = s.toCharArray(); char[] tt = t.toCharArray();
近期评论