205. Isomorphic Strings

Problem

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

Note:
You may assume both s and t have the same length.

Example

Input: s = "egg", t = "add"
Output: true
Input: s = "foo", t = "bar"
Output: false
Input: s = "paper", t = "title"
Output: true

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        map<char,char> dict;
        int exist[127] = {0};
        for(int i=0;i<s.size();i++){
            if(dict.find(s[i])!=dict.end()){
                if(dict[s[i]]!=t[i])
                    return false;
            }else{
                dict[s[i]] = t[i];
                if(exist[t[i]]!=0)
                    return false;
                exist[t[i]] = 1;
            }
        }
        return true;
    }
};

Result

205. Isomorphic Strings.png
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,826評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,026評論 0 23
  • 恐慌的小白,讀了你的來信,我也在回想曾經的自己。我不能說是給你建議,我只能說如果我是你,我會怎樣做? 我會辭職,一...
    游牧一方閱讀 372評論 0 2
  • 風花雪月,是歷代文人墨客歌頌與寄情的對象。 我的家鄉(xiāng)在華北平原,初冬時分,一般都先下一場小雪,或者雨轉成雪,這時落...
    行云如是說閱讀 571評論 0 0
  • 春 就這樣,靜靜的來到了 萬物開始復蘇 花兒競相開放 心情無比的舒暢 像初戀時的心動 更像初生新生命時的喜悅 春,...
    一言一諾閱讀 239評論 0 1

友情鏈接更多精彩內容