Skip to content

Commit 48c12ee

Browse files
authored
Merge pull request #1307 from ivan1016017/jun19
adding algo
2 parents 373bd7b + b745957 commit 48c12ee

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

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+
4+
class Solution:
5+
def wordPattern(self, pattern: str, s: str) -> bool:
6+
7+
dic_answer = dict()
8+
9+
words = s.split(' ')
10+
11+
if len(words) != len(pattern) or len(set(words)) != len(set(pattern)):
12+
return False
13+
else:
14+
for k, v in enumerate(words):
15+
if v in dic_answer:
16+
if dic_answer[v] != pattern[k]:
17+
return False
18+
else:
19+
dic_answer[v] = pattern[k]
20+
21+
return True
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_17\
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)

0 commit comments

Comments
 (0)