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.
Note:
You may assume the string contains only lowercase alphabets.
题意:两个字符串中,相同字符出现相同次数,并且没有多余字符,则就是Valid Anagram。
public class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
String[] sArray = s.split("");
Arrays.sort(sArray);
String[] tArray = t.split("");
Arrays.sort(tArray);
for (int i = 0; i < s.length(); ++i) {
if (!sArray[i].equals(tArray[i])) {
return false;
}
}
return true;
}
}