[leetcode][290]Word Pattern
来源:互联网 发布:新版淘宝淘抢购在哪 编辑:程序博客网 时间:2024/06/04 23:55
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:
- pattern =
"abba"
, str ="dog cat cat dog"
should return true. - pattern =
"abba"
, str ="dog cat cat fish"
should return false. - pattern =
"aaaa"
, str ="dog cat cat dog"
should return false. - 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
0 0
- leetcode 290: Word Pattern
- [leetcode 290]Word Pattern
- LeetCode 290 Word Pattern
- [LeetCode 290] Word Pattern
- leetcode 290 Word Pattern
- [leetcode 290] Word Pattern
- LeetCode 290 Word Pattern
- 【LEETCODE】290-Word Pattern
- LeetCode 290:Word Pattern
- leetcode 290 Word Pattern
- leetcode 290: Word Pattern
- 【LeetCode-290】Word Pattern
- LeetCode 290 Word Pattern
- leetcode 290:Word Pattern
- LeetCode[290] Word Pattern
- Leetcode 290 Word Pattern
- [leetcode][290]Word Pattern
- leetcode-290-Word Pattern
- swift中添加模糊图片
- SpannableString属性详解
- 利用 Spring MVC 上传多文件到指定目录 spring upload files
- 简易nginx+tomcat环境的搭建
- java的IO流----将多个拆分的子文件合并为一个目标文件
- [leetcode][290]Word Pattern
- Spring Cloud Config
- 外观模式(门面模式)---脸有时比才华更重要
- 最近公共祖先 LCA 倍增算法
- python_女友排行榜
- HTML 中插入视频播放窗口
- 模板方法模式
- Spring+Struts2+Mybatis在Intellij下的整合
- JPA 菜鸟教程 21 JPA2.0 @MapKeyColumn