-
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.
- Loading branch information
Showing
3 changed files
with
40 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
Sep 2, 2024 | ||
|
||
* solve "290. Word Pattern" | ||
|
||
Sep 1, 2024 | ||
|
||
complete top interview 150 | ||
|
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,20 @@ | ||
class Solution: | ||
def wordPattern(self, pattern: str, s: str) -> bool: | ||
words = s.split() | ||
|
||
if len(pattern) != len(words): | ||
return False | ||
|
||
letter_to_word: dict[str, str] = {} | ||
word_to_letter: dict[str, str] = {} | ||
|
||
for letter, word in zip(pattern, s.split()): | ||
if letter_to_word.get(letter, word) != word: | ||
return False | ||
if word_to_letter.get(word, letter) != letter: | ||
return False | ||
|
||
letter_to_word[letter] = word | ||
word_to_letter[word] = letter | ||
|
||
return True |
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,16 @@ | ||
import pytest | ||
|
||
from src.word_pattern import Solution | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"expected,pattern,s", | ||
( | ||
(True, "abba", "dog cat cat dog"), | ||
(False, "abba", "dog cat cat fish"), | ||
(False, "aaaa", "dog cat cat dog"), | ||
(False, "aaa", "aa aa aa aa"), | ||
), | ||
) | ||
def test_solution(expected, pattern, s): | ||
assert expected is Solution().wordPattern(pattern, s) |