Skip to content

Commit

Permalink
Create: 0290-word-patterns.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Jan 16, 2023
1 parent fa2d4c3 commit 3f70fd9
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions csharp/0290-word-pattern.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}

0 comments on commit 3f70fd9

Please sign in to comment.