LeetCode之Isomorphic Strings

/*從s->t映射可以,用hash記住每個映射,判斷是否能從s->t進行映射。
反過來,再判斷是否能從t->進行映射,只有能進s->t和t->的映射,纔是同構的。*/
class Solution {
public:
    bool isIsomorphic(string s, string t){
        return s2t(s, t) && s2t(t, s);
    }

    bool s2t(string s, string t) {
        unordered_map<char, char> replaced;
        for(int i = 0; i < s.size(); ++i){
            if(replaced.find(s[i]) == replaced.end()){
                replaced[s[i]] = t[i];
            }
            else{
                if(replaced[s[i]] != t[i]) return false;
            }
        }
        return true;
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章