Skip to content

Commit 848cb87

Browse files
authored
Merge pull request #1131 from ivan1016017/december24
adding word pattern
2 parents 7b85015 + d1bb71b commit 848cb87

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import defaultdict
4+
5+
class Solution:
6+
def wordPattern(self, pattern: str, s: str) -> bool:
7+
8+
dic_answer = defaultdict(str)
9+
words = s.split(' ')
10+
11+
if len(words) != len(pattern) or len(set(pattern)) != len(set(words)):
12+
return False
13+
else:
14+
for k, v in enumerate(pattern):
15+
if v in dic_answer:
16+
if dic_answer[v] != words[k]:
17+
return False
18+
else:
19+
dic_answer[v] = words[k]
20+
return True
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_12\
3+
.word_pattern import Solution
4+
5+
class WordPatternTestCase(unittest.TestCase):
6+
7+
def test_is_word_pattern(self):
8+
solution = Solution()
9+
output = solution.wordPattern(pattern="abba", s="dog cat cat dog")
10+
self.assertTrue(output)
11+
12+
def test_is_no_word_pattern_one(self):
13+
solution = Solution()
14+
output = solution.wordPattern(pattern="abc", s="dog cat cat fish")
15+
self.assertFalse(output)
16+
17+
def test_is_no_word_pattern_two(self):
18+
solution = Solution()
19+
output = solution.wordPattern(pattern="aa", s="dog cat cat fish")
20+
self.assertFalse(output)
21+
22+

0 commit comments

Comments
 (0)