Skip to content

Commit 563cd95

Browse files
authored
Merge pull request #1166 from ivan1016017/january28
adding word pattern
2 parents b84c7e8 + 473e0f2 commit 563cd95

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
21+
return True
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_13\
3+
.word_pattern import Solution
4+
5+
6+
class WordPatternTestCase(unittest.TestCase):
7+
8+
def test_is_word_pattern(self):
9+
solution = Solution()
10+
output = solution.wordPattern(pattern="abba", s="dog cat cat dog")
11+
self.assertTrue(output)
12+
13+
def test_is_no_word_pattern_one(self):
14+
solution = Solution()
15+
output = solution.wordPattern(pattern="abc", s="dog cat cat fish")
16+
self.assertFalse(output)
17+
18+
def test_is_no_word_pattern_two(self):
19+
solution = Solution()
20+
output = solution.wordPattern(pattern="aa", s="dog cat cat fish")
21+
self.assertFalse(output)

0 commit comments

Comments
 (0)