Skip to content

Commit 03a0d0c

Browse files
authored
Create word_pattern.py
1 parent 5089cb0 commit 03a0d0c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

word_pattern.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
"""
3+
@param pattern: a string, denote pattern string
4+
@param teststr: a string, denote matching string
5+
@return: an boolean, denote whether the pattern string and the matching string match or not
6+
"""
7+
def wordPattern(self, pattern, teststr):
8+
# write your code here
9+
di = {}
10+
pattern_mapping = []
11+
_id = 0
12+
for c in pattern:
13+
if c not in di:
14+
di[c] = _id
15+
_id += 1
16+
pattern_mapping.append(di[c])
17+
_id = 0
18+
di.clear()
19+
str_mapping = []
20+
words = teststr.split(' ')
21+
for word in words:
22+
if word not in di:
23+
di[word] = _id
24+
_id += 1
25+
str_mapping.append(di[word])
26+
return str_mapping == pattern_mapping
27+
28+
# easy: https://www.lintcode.com/problem/word-pattern/

0 commit comments

Comments
 (0)