1. 程式人生 > >C++ Leetcode初級演算法之有效的字母異位詞

C++ Leetcode初級演算法之有效的字母異位詞

給定兩個字串 s 和 t ,編寫一個函式來判斷 t 是否是 s 的一個字母異位詞。

示例 1:

輸入: s = “anagram”, t = “nagaram”
輸出: true
示例 2:

輸入: s = “rat”, t = “car”
輸出: false
說明:
你可以假設字串只包含小寫字母。

進階:
如果輸入字串包含 unicode 字元怎麼辦?你能否調整你的解法來應對這種情況?

class Solution {
public:
    bool isAnagram(string s, string t) {
        map<char,int> smp;
        map<char,int> tmp;
        for(auto c : s)
            smp[c]++;
        for(auto c : t)
            tmp[c]++;
        if(smp == tmp)
            return true;
        return false;
    }
};