-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsomorphicStrings.cs
More file actions
29 lines (26 loc) · 982 Bytes
/
Copy pathIsomorphicStrings.cs
File metadata and controls
29 lines (26 loc) · 982 Bytes
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
namespace algorithms.LeetCode;
public class IsomorphicStrings {
// Given two strings s and t, determine if they are isomorphic.
// Two strings s and t 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.
public bool IsIsomorphic(string s, string t) {
var distinctS = s.Distinct();
var distinctT = t.Distinct();
if(distinctS.Count() != distinctT.Count()) return false;
var dict = new Dictionary<char, char>();
for(int i = 0; i < s.Length; i++)
{
if(!dict.ContainsKey(s[i]))
{
dict[s[i]] = t[i];
continue;
}
if(dict[s[i]] != t[i])
{
return false;
}
}
return true;
}
}