Skip to content

Commit 84ddebf

Browse files
solves word pattern in python
1 parent 6e7e8c6 commit 84ddebf

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
| 276 | 🔒 [Paint Fence](https://leetcode.com/problems/paint-fence) | Easy | |
8787
| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/FirstBadVersion.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/first_bad_version.py) |
8888
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MoveZeros.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/move_zeroes.py) |
89-
| 290 | [Word Pattern](https://leetcode.com/problems/word-pattern) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/WordPattern.java) |
89+
| 290 | [Word Pattern](https://leetcode.com/problems/word-pattern) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/WordPattern.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/word_pattern.py) |
9090
| 292 | [Nim Game](https://leetcode.com/problems/nim-game) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/NimGame.java) |
9191
| 293 | [Flip Game](https://leetcode.com/problems/flip-game) | Easy | |
9292
| 299 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/BullsAndCows.java) |

python/word_pattern.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def wordPattern(self, pattern: str, s: str) -> bool:
3+
words = s.split()
4+
if len(words) != len(pattern):
5+
return False
6+
mapping = {}
7+
used_words = set()
8+
for character, word in zip(pattern, words):
9+
if character in mapping:
10+
if mapping[character] != word:
11+
return False
12+
elif word in used_words:
13+
return False
14+
else:
15+
mapping[character] = word
16+
used_words.add(word)
17+
return True
18+
19+
20+
print(Solution().wordPattern('abba', 'dog dog dog dog'))

0 commit comments

Comments
 (0)