10000 290.Word Pattern · zwxalgorithm/LeetCode-3@a896b69 · GitHub
[go: up one dir, main page]

Skip to content

Commit a896b69

Browse files
committed
290.Word Pattern
1 parent ec2b88a commit a896b69

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Hash Table/Easy/290.Word Pattern.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 题目
2+
3+
根据pattern和str,找到是否符合这个pattern匹配模式。
4+
5+
**举例:**
6+
7+
* pattern = "abba", str = "dog cat cat dog" should return true.
8+
* pattern = "abba", str = "dog cat cat fish" should return false.
9+
* pattern = "aaaa", str = "dog cat cat dog" should return false.
10+
* pattern = "abba", str = "dog dog dog dog" should return false.
11+
12+
**注意:**
13+
14+
假设pattern只包含小写字母,而str包含由空格分隔的小写字母。
15+
16+
# 思路
17+
18+
对于python,巧妙使用map和find函数即可。
19+
20+
# 代码
21+
22+
**python:**
23+
24+
``` python
25+
class Solution:
26+
def wordPattern(self, pattern, str):
27+
"""
28+
:type pattern: str
29+
:type str: str
30+
:rtype: bool
31+
"""
32+
t = str.split(' ')
33+
return list(map(pattern.find, pattern)) == list(map(t.index, t))
34+
```
35+
36+

0 commit comments

Comments
 (0)
0