205. Isomorphic Strings

2018-08-13  本文已影响0人  SilentDawn

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
上一篇 下一篇

猜你喜欢

热点阅读