Changchun Master Li

LeetCode Word Pattern

2015-11-04

leetcode

[lang:python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
length = len(pattern)
strings = str.split()
if len(strings) != length:
return False
"pattern strings 检查长度是否匹配"

table = dict()
for letter, word in zip(pattern, strings):
if letter in table:
if word == table[letter]:
continue
else:
return False
table[letter] = word
del table
"每一个单词都会对应唯一的字母"

table = dict()
for letter, word in zip(pattern, strings):
if word in table:
if letter == table[word]:
continue
else:
return False
table[word] = letter
"每一个字母都会对应唯一的单词"

return True
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章