# [leetcode][290]Word Pattern

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Examples:

1. pattern = "abba", str = "dog cat cat dog" should return true.
2. pattern = "abba", str = "dog cat cat fish" should return false.
3. pattern = "aaaa", str = "dog cat cat dog" should return false.
4. pattern = "abba", str = "dog dog dog dog" should return false.

Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

class Solution(object):
def wordPattern(self, pattern, astr):
"""
:type pattern: str
:type str: astr
:rtype: bool
"""
temp  = astr.split()
if len(temp) != len(pattern):
return False
p = {}
q = {}
for a,b in zip(pattern, temp):
if b in p:
if a != p[b]:
return False
else:
p[b] = a
if a in q:
if b != q[a]:
return False
else:
q[a] = b
return True



• 本文已收录于以下专栏：

## Leetcode290. Word Pattern & 205. Isomorphic Strings

Given a pattern and a string str, find if str follows the same pattern.Here follow means a full matc...

## LeetCode（290） Word Pattern

• fly_yr
• 2015年11月23日 13:33
• 378

## <LeetCode OJ> 290. Word Pattern

290. Word Pattern My Submissions Question Total Accepted: 24989 Total Submissions: 89440 Difficu...

## LeetCode 290 word pattern 的一点疑惑

LeetCode第290题，题目大致意思是给定两个字符串，判断字符串2的模式是否与字符串1相同，也就是说对应位置的字符是否相同。如： Examples: 1.pattern = "abba", s...

## LeetCode 290 Word Pattern（单词模式）（istringstream、vector、map）（*）

• NoMasp
• 2016年01月30日 15:43
• 1720

## [Leetcode] 290. Word Pattern 解题报告

举报原因： 您举报文章：[leetcode][290]Word Pattern 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)