242. Valid Anagram
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.
题目:
给出字符串s,判断t字符串是不是由s字符串的字符组成的;
问题抽象为,判断s和t中各个字符的数量是不是相等;
用数组来存储每个字符的数量;
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length())
return false;
int count[]=new int[26];
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
count[c-'a']++;
}
for(int i=0;i<t.length();i++){
char c=t.charAt(i);
count[c-'a']--;
}
for(int j=0;j<26;j++){
if(count[j]!=0)
return false;
}
return true;
}
}