Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2045 from alexprudhomme/0290-word-patt…
Browse files Browse the repository at this point in the history
…ern.cs

Create: 0290-word-patterns.cs
  • Loading branch information
tahsintunan authored Jan 16, 2023
2 parents 3838bb5 + 3f70fd9 commit d142177
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 d142177

Please sign in to comment.