-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0205-IsomorphicStrings.cs
35 lines (32 loc) · 1.01 KB
/
0205-IsomorphicStrings.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
28
29
30
31
32
33
34
35
//-----------------------------------------------------------------------------
// Runtime: 72ms
// Memory Usage: 21.6 MB
// Link: https://leetcode.com/submissions/detail/260098104/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0205_IsomorphicStrings
{
public bool IsIsomorphic(string s, string t)
{
if (s.Length != t.Length) return false;
var map = new Dictionary<char, char>(26);
var visited = new HashSet<char>(26);
for (int i = 0; i < s.Length; i++)
{
if (map.ContainsKey(s[i]))
{
if (map[s[i]] != t[i]) return false;
}
else
{
if (visited.Contains(t[i])) return false;
map.Add(s[i], t[i]);
visited.Add(t[i]);
}
}
return true;
}
}
}