給定兩個字符串 s 和 t ,編寫一個函數來判斷 t 是否是 s 的一個字母異位詞。
輸入: s = "anagram", t = "nagaram"
輸出: true
輸入: s = "rat", t = "car"
輸出: false
說明:
你可以假設字符串只包含小寫字母。
如果輸入字符串包含 unicode 字符怎么辦?你能否調整你的解法來應對這種情況?
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> count1(128, 0);
vector<int> count2(128, 0);
for(char ch : s){
count1[ch]++;
}
for(char ch : t){
count2[ch]++;
}
for(int i = 0; i < 128; i++){
if(count1[i] != count2[i]){
return false;
}
}
return true;
}
};
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。