forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2045 from alexprudhomme/0290-word-patt…
…ern.cs Create: 0290-word-patterns.cs
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |