Skip to content

Commit d142177

Browse files
authored
Merge pull request #2045 from alexprudhomme/0290-word-pattern.cs
Create: 0290-word-patterns.cs
2 parents 3838bb5 + 3f70fd9 commit d142177

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

csharp/0290-word-pattern.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public bool WordPattern(string pattern, string s) {
3+
var words = s.Split(' ');
4+
if (words.Length != pattern.Length) return false;
5+
6+
Dictionary<char,string> charToWord = new Dictionary<char, string>();
7+
Dictionary<string, char> wordToChar = new Dictionary<string, char>();
8+
9+
for (int i = 0; i < pattern.Length; i++) {
10+
char c = pattern[i];
11+
string w = words[i];
12+
if (charToWord.ContainsKey(c) && (charToWord[c] != w)) {
13+
return false;
14+
}
15+
if(wordToChar.ContainsKey(w) && (wordToChar[w] != c)) {
16+
return false;
17+
}
18+
charToWord[c] = w;
19+
wordToChar[w] = c;
20+
}
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)