forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
27 lines (26 loc) · 826 Bytes
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System.Collections.Generic;
public class Solution {
public bool IsIsomorphic(string s, string t) {
if (s.Length != t.Length) return false;
var dict1 = new Dictionary<char, char>();
var dict2 = new Dictionary<char, char>();
for (var i = 0; i < s.Length; ++i)
{
char mapping1;
char mapping2;
var found1 = dict1.TryGetValue(s[i], out mapping1);
var found2 = dict2.TryGetValue(t[i], out mapping2);
if (found1 ^ found2) return false;
if (!found1)
{
dict1.Add(s[i], t[i]);
dict2.Add(t[i], s[i]);
}
else if (mapping1 != t[i] || mapping2 != s[i])
{
return false;
}
}
return true;
}
}