1、题目来源:点击打开链接
2、题目:
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
3、也是将字母做为索引,然后进行计数:
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length())
return false;
char [] str1=s.toCharArray();
char [] str2=t.toCharArray();
int sum=0;
int[] count=new int[26];
for(char c:str1){
count[c-'a']++;
}
for(char c:str2){
if(--count[c-'a']<0)
return false;
}
return true;
}
}
4、和题目一样:
leetcode-383. Ransom Note