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.
public class Solution {
public bool IsIsomorphic(string s, string t) {
if (s.Length != t.Length) {
return false;
}
Dictionary<char, int> set = new Dictionary<char, int>();
List<int> parS = new List<int>();
int num = 1;
foreach(var i in s) {
int val;
set.TryGetValue(i, out val);
if (val != 0) {
parS.Add(set[i]);
} else {
set[i] = num;
parS.Add(set[i]);
num++;
}
}
List<int> parT = new List<int>();
set.Clear();
num = 1;
foreach (var i in t) {
int val;
set.TryGetValue(i, out val);
if (val != 0) {
parT.Add(set[i]);
} else {
set[i] = num;
parT.Add(set[i]);
num++;
}
}
for (var i = 0; i < parS.Count; i++) {
if (parT[i] != parS[i]) {
return false;
}
}
return true;
}
}