题目链接:https://leetcode-cn.com/problems/valid-anagram/
题目如下:
class Solution {
public boolean isAnagram(String s, String t) {
//思路:既然是异位;则两个长度一定是相同的,不相同则必然有错
if(s.length()==t.length()) ;
else return false;
//注:在hashmap中对应的是<key-value>
//注:在hashmap放元素是put,在hashset中为add
HashMap<Character,Integer> hashmap=new HashMap<Character,Integer>();
for(int i=0;i<s.length();i++){
int count=hashmap.getOrDefault(s.charAt(i),0)+1;
hashmap.put(s.charAt(i),count);
}
for(int i=0;i<t.length();i++){
if(hashmap.containsKey(t.charAt(i))==true){
int count=hashmap.get(t.charAt(i));
if(count==1) hashmap.remove(t.charAt(i));
else hashmap.put(t.charAt(i),count-1);
}
else return false;
}
return true;
}
}