// Two strings are isomorphic if characters in one can be replaced to get the other,
// while preserving the order.
#include <iostream>
#include <string>
#include <unordered_map>
bool areIsomorphic(const std::string& s, const std::string& t) {
if (s.size() != t.size()) return false;
std::unordered_map<char, char> mapST;
std::unordered_map<char, char> mapTS;
for (size_t i = 0; i < s.size(); i++) {
char c1 = s[i], c2 = t[i];
// Check mapping from s → t
if (mapST.count(c1)) {
if (mapST[c1] != c2) return false;
} else {
mapST[c1] = c2;
}
// Check mapping from t → s
if (mapTS.count(c2)) {
if (mapTS[c2] != c1) return false;
} else {
mapTS[c2] = c1;
}
}
return true;
}
int main() {
std::cout << std::boolalpha;
std::cout << areIsomorphic("egg", "add") << "\n";
std::cout << areIsomorphic("foo", "bar") << "\n";
std::cout << areIsomorphic("paper", "title") << "\n";
}
/*
run:
true
false
true
*/