[leetcode: Python]290.Word Pattern
2017-05-13 17:13
155 查看
题目:
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.
题意:
如上所述,注意,模式只包含小写字母,分隔符是空格。
方法一:性能58ms
方法二:性能32ms
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.
题意:
如上所述,注意,模式只包含小写字母,分隔符是空格。
方法一:性能58ms
class Solution(object): def wordPattern(self, pattern, str): """ :type pattern: str :type str: str :rtype: bool """ s = str.split(' ') if len(pattern) != len(s): return False h = {} mapval = [] for i in range(len(pattern)): if pattern[i] in h: if h[pattern[i]] != s[i]: return False else: if s[i] in mapval: return False h[pattern[i]] = s[i] mapval.append(s[i]) return True
方法二:性能32ms
class Solution(object): def wordPattern(self, pattern, str): """ :type pattern: str :type str: str :rtype: bool """ d = {} d1 = {} l = str.split(" ") if len(l) != len(pattern): return False for i in range(len(pattern)): if pattern[i] not in d: d[pattern[i] ] = l[i] if l[i] not in d1: d1[l[i]] = pattern[i] else: return False else: if l[i] != d[pattern[i]]: return False return True
相关文章推荐
- 【LeetCode with Python】 Wildcard Matching
- 【LeetCode with Python】 Generate Parentheses
- 【LeetCode with Python】 Remove Duplicates from Sorted Array
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- 【LeetCode with Python】 Maximum Depth of Binary Tree
- 【LeetCode with Python】 Binary Tree Inorder Traversal
- 【LeetCode with Python】 Pow(x, n)
- 【LeetCode with Python】 Maximum Subarray
- 【LeetCode with Python】 Insertion Sort List
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Reverse Words in a String
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- 【LeetCode with Python】 String to Integer (atoi)
- 【LeetCode with Python】 LRU Cache
- 【LeetCode with Python】 Remove Element
- 【LeetCode with Python】 Validate Binary Search Tree
- 【LeetCode with Python】 Pascal's Triangle
- 【LeetCode with Python】 Balanced Binary Tree
- 【LeetCode with Python】 Minimum Depth of Binary Tree