# 205. Isomorphic Strings

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.

Example 1:

Input: s = "egg", t = "add"
Output: true

Example 2:

Input: s = "foo", t = "bar"
Output: false

Example 3:

Input: s = "paper", t = "title"
Output: true

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

# Solution

Approach 1: Hash table: char -> char or char -> position.

# Code (Python)

Approach 1:

# Code (C++)

Approach 1:

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        if (s.size() != t.size()) return false;
        unordered_map<char,char> stMap;
        unordered_map<char,char> tsMap;
        for (int i = 0; i < s.size(); ++i)
        {
            if ((stMap.find(s[i]) != stMap.end() && stMap[s[i]] != t[i]) ||
                (tsMap.find(t[i]) != tsMap.end() && tsMap[t[i]] != s[i]))
                return false;
            stMap[s[i]] = t[i];
            tsMap[t[i]] = s[i];
        }
        return true;
    }
};

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        if (s.size() != t.size()) return false;
        int stMap[256] = {0};
        int tsMap[256] = {0};
        for (int i = 0; i < s.size(); ++i)
        {
            if (stMap[s[i]] != tsMap[t[i]])
                return false;
            stMap[s[i]] = i + 1; // the default is 0.
            tsMap[t[i]] = i + 1;
        }
        return true;
    }
};