forked from super30admin/Hashing-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem3_WordPattern.cs
38 lines (34 loc) · 1.35 KB
/
Problem3_WordPattern.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
36
37
38
// Time Complexity :O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
//Approach: Using dictionary to map each char of string 's' to corresponding char of string 't'.
//check the dictionary to find mapping for a char. If it exists check the mapped char and the iterating
//char are same, if they are different then the strings are not isomorphic.
public class Solution {
public bool WordPattern(string pattern, string s) {
if(pattern == null || s == null) return false;
var patternChars = pattern.ToCharArray();
string[] words = s.Split(' ');
if(patternChars.Length != words.Length) return false;
HashSet<string> mappedWords = new HashSet<string>();
Dictionary<char, string> hashMap = new Dictionary<char, string>();
for(int i = 0; i < patternChars.Length; i++) {
if(hashMap.TryGetValue(patternChars[i], out var value)) {
if(value != words[i])
{
return false;
}
}
else {
if(mappedWords.Contains(words[i])) {
return false;
}
else {
hashMap.Add(patternChars[i], words[i]);
mappedWords.Add(words[i]);
}
}
}
return true;
}
}