注:此博客不再更新,所有最新文章将发表在个人独立博客limengting.site。分享技术,记录生活,欢迎大家关注
Given two strings s and t, write a function to determine if t i()s 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.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
public class Solution {
public boolean isAnagram(String s, String t) {
int[] alp = new int[26];
for (int i = 0; i < s.length(); i ++)
alp[s.charAt(i) - 'a'] ++;
for (int j = 0; j < t.length(); j ++)
alp[t.charAt(j) - 'a'] --;
for (int i : alp)
if (i != 0) return false;
return true;
}
}