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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,199评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,591评论 0 23
  • 恐慌的小白,读了你的来信,我也在回想曾经的自己。我不能说是给你建议,我只能说如果我是你,我会怎样做? 我会辞职,一...
    游牧一方阅读 2,682评论 0 2
  • 风花雪月,是历代文人墨客歌颂与寄情的对象。 我的家乡在华北平原,初冬时分,一般都先下一场小雪,或者雨转成雪,这时落...
    行云如是说阅读 3,477评论 0 0
  • 春 就这样,静静的来到了 万物开始复苏 花儿竞相开放 心情无比的舒畅 像初恋时的心动 更像初生新生命时的喜悦 春,...
    一言一诺阅读 1,612评论 0 1

友情链接更多精彩内容