forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0290-word-pattern.cs
More file actions
23 lines (21 loc) · 771 Bytes
/
0290-word-pattern.cs
File metadata and controls
23 lines (21 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Solution {
public bool WordPattern(string pattern, string s) {
var words = s.Split(' ');
if (words.Length != pattern.Length) return false;
Dictionary<char,string> charToWord = new Dictionary<char, string>();
Dictionary<string, char> wordToChar = new Dictionary<string, char>();
for (int i = 0; i < pattern.Length; i++) {
char c = pattern[i];
string w = words[i];
if (charToWord.ContainsKey(c) && (charToWord[c] != w)) {
return false;
}
if(wordToChar.ContainsKey(w) && (wordToChar[w] != c)) {
return false;
}
charToWord[c] = w;
wordToChar[w] = c;
}
return true;
}
}