给定两个字符串 s 和 t,判断它们是否是同构的。
如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。
所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
示例 1: 输入: s = "egg", t = "add" 输出: true 示例 2: 输入: s = "foo", t = "bar" 输出: false 示例 3: 输入: s = "paper", t = "title" 输出: true
说明:
你可以假设 s 和 t 具有相同的长度。
public bool IsIsomorphic(string s, string t) {
Dictionary<char,int> dic1 = new Dictionary<char,int>();
Dictionary<char,int> dic2 = new Dictionary<char,int>();
int index1 = -1;
int index2 = -1;
for(int i = 0;i<s.Length;i++)
{
index1 = -1;
index2 = -1;
if(dic1.ContainsKey(s[i]))
{
index1 = dic1[s[i]];
}
else
{
dic1.Add(s[i],i);
}
if(dic2.ContainsKey(t[i]))
{
index2 = dic2[t[i]];
}
else
{
dic2.Add(t[i],i);
}
if(index1 != index2)
{
return false;
}
}
return true;
}