// Two strings are isomorphic if characters in one can be replaced to get the other,
// while preserving the order.
using System;
using System.Collections.Generic;
class Program
{
static bool AreIsomorphic(string s, string t) {
if (s.Length != t.Length) return false;
var mapST = new Dictionary<char, char>();
var mapTS = new Dictionary<char, char>();
for (int i = 0; i < s.Length; i++) {
char c1 = s[i];
char c2 = t[i];
// Check mapping from s → t
if (mapST.ContainsKey(c1)) {
if (mapST[c1] != c2) return false;
}
else {
mapST[c1] = c2;
}
// Check mapping from t → s
if (mapTS.ContainsKey(c2)) {
if (mapTS[c2] != c1) return false;
}
else {
mapTS[c2] = c1;
}
}
return true;
}
static void Main()
{
Console.WriteLine(AreIsomorphic("egg", "add"));
Console.WriteLine(AreIsomorphic("foo", "bar"));
Console.WriteLine(AreIsomorphic("paper", "title"));
}
}
/*
run:
True
False
True
*/