給定兩個字符串 s 和 t ,編寫一個函數來判斷 t 是否是 s 的一個字母異位詞。
示例 1:
輸入: s = "anagram", t = "nagaram" 輸出: true
示例 2:
輸入: s = "rat", t = "car" 輸出: false
說明:
你可以假設字符串只包含小寫字母。
進階:
如果輸入字符串包含 unicode 字符怎么辦?你能否調整你的解法來應對這種情況?
class Solution {
public boolean isAnagram(String s, String t) {
char[] ch1=s.toCharArray();
Arrays.sort(ch1);
char[] ch2=t.toCharArray();
Arrays.sort(ch2);
return Arrays.equals(ch1,ch2);
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。