public class Solution {
public boolean isAnagram(String s, String t) {
if (s == null || t == null || s.length() != t.length()) {
return false;
}
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
String sc = s.substring(i, i + 1);
String tc = t.substring(i, i + 1);
if (map.containsKey(sc)) {
map.put(sc, map.get(sc) + 1);
} else {
map.put(sc, 1);
}
if (map.containsKey(tc)) {
map.put(tc, map.get(tc) - 1);
} else {
//map.put(tc, 1);
map.put(tc, -1);
}
}
for (Map.Entry<String, Integer> entry: map.entrySet()) {
if (entry.getValue() != 0) {
return false;
}
}
return true;
}
}
Valid Anagram
最新推荐文章于 2024-01-10 12:44:07 发布