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.
public class Solution {
public boolean isAnagram(String s, String t) {
int[] map = new int[26];
char[] cs = s.toCharArray();
for(char c : cs) {
int x = c - 'a';
map[x] += 1;
}
cs = t.toCharArray();
for(char c : cs) {
int x = c - 'a';
if(map[x] == 0) {
return false;
}
map[x] -= 1;
}
for(int x : map) {
if(x != 0) {
return false;
}
}
return true;
}
}
//是否字母顺序不一样,用一个哈希表记录其中一个字符串的每一个字符及
//出现的次数。用另一个字符串的字符当作key获取value判断是否存在。
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦