原题链接:https://leetcode.com/problems/isomorphic-strings/
Description
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.
For example,
Given “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
Note:
You may assume both s and t have the same length.
class Solution {
private:
typedef unordered_map<char, char> UMP;
public:
bool isIsomorphic(string s, string t) {
if (s == t) return true;
UMP A, B;
int n = s.length();
for (int i = 0; i < n; i++) {
char x1 = s[i], x2 = t[i];
if (A.find(x1) == A.end()) A[x1] = x2;
else if (A[x1] != x2) return false;
if (B.find(x2) == B.end()) B[x2] = x1;
else if (B[x2] != x1) return false;
}
return true;
}
};