We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 3838bb5 + 3f70fd9 commit d142177Copy full SHA for d142177
csharp/0290-word-pattern.cs
@@ -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
17
18
+ charToWord[c] = w;
19
+ wordToChar[w] = c;
20
21
+ return true;
22
23
+}
0 commit comments