diff --git a/csharp/0290-word-pattern.cs b/csharp/0290-word-pattern.cs new file mode 100644 index 000000000..61b1441a9 --- /dev/null +++ b/csharp/0290-word-pattern.cs @@ -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 charToWord = new Dictionary(); + Dictionary wordToChar = new Dictionary(); + + 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; + } +} \ No newline at end of file