-
Notifications
You must be signed in to change notification settings - Fork 0
/
290.py
46 lines (35 loc) · 1.04 KB
/
290.py
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
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
words = str.split(" ")
if len(words) != len(pattern):
return False
p_d = dict()
w_d = dict()
i = 0
while i < len(pattern):
if pattern[i] in p_d:
if p_d[pattern[i]] != words[i]:
return False
else:
p_d[pattern[i]] = words[i]
else:
p_d[pattern[i]] = words[i]
if words[i] in w_d:
if w_d[words[i]] != pattern[i]:
return False
else:
w_d[words[i]] = pattern[i]
else:
w_d[words[i]] = pattern[i]
i += 1
return True
if __name__ == "__main__":
print Solution().wordPattern("jquery", "jquery")