290. Word Pattern [easy] (Python)

题目链接

https://leetcode.com/problems/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.

题目翻译

给定一个模式(pattern)和一个字符串(str),判断字符串是否匹配该模式。这里匹配指的是完全匹配,是pattern中的字符和str中的非空单词之间的双射。

这题与另一题非常类似,解法基本一致,可参看:

205. Isomorphic Strings

思路方法

思路一

同时遍历pattern的每个字符,和str中的每个word,建立它们的映射关系,当出现违背映射关系的情况时,可直接返回false。另外,由于是双射,pattern中的不同字符不能映射到相同的word,所以也要做这方面的检查。

代码

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
        hashmap = {}
        mapval = {}
        for i in xrange(len(pattern)):
            if pattern[i] in hashmap:
                if hashmap[pattern[i]] != words[i]:
                    return False
            else:
                if words[i] in mapval:
                    return False
                hashmap[pattern[i]] = words[i]
                mapval[words[i]] = True
        return True

思路二

对于pattern和str,分别用一个数组记录每个字符或word第一次出现的位置。当同时遍历pattern和str时,如果发现它们在某一位置的字符或word第一次出现的位置不同,则返回false。

代码

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
        return map(pattern.find, pattern) == map(words.index, words)

思路三

根据题目的描述,pattern有多少种不同的字符,str也有多少种不同的word。如果我们将映射写成字符对的形式,比如 (‘a’,’dog’) 表示pattern中字符’a’映射到str中’dog’,那么映射的个数与pattern中字符的种类数相同。

所以该方法本质上也是判断映射是不是双射。

代码

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
        return len(set(pattern)) == len(set(words)) == len(set(zip(pattern, words)))

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!

转载请注明:http://blog.csdn.net/coder_orz/article/details/51693647

时间: 2024-08-14 16:37:42

290. Word Pattern [easy] (Python)的相关文章

290. Word Pattern

/* * 290. Word Pattern * 2016-7-2 by Mingyang * 这里加上了没有containsValue,因为这里如果abba 和 dog dog dog dog通不过, * 因为a已经包含dog了,b却也包含了dog,解决方法就是value不能重复 * 直接用containsValue就好了 */ public static boolean wordPattern(String pattern, String str) { String[] array=str.

LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pattern = "abba". str = "dog cat cat dog" 应该返回真. pattern = "abba", str = "dog cat cat fish" 应该返回假. pattern = "aaa

【leetcode?python】 290. Word Pattern

class Solution(object):    def wordPattern(self, pattern, str):        """        :type pattern: str        :type str: str        :rtype: bool        """        tag=0        tagdic={}        tagList=[]        i=0        while

【一天一道LeetCode】#290. Word Pattern

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 pa

LeetCode 290. Word Pattern

不符合的情况有三种:1.pattern和str长度不符   2.pattern中不同字母指向str中同一词(a.b->同一词)   3.同一字母指向不同词(a->不同词) 1 class Solution { 2 public: 3 vector<string> split(string str){ 4 vector<string> res; 5 string tmp = ""; 6 for(int i = 0; i < str.length(

290. Word Pattern(LeetCode)

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 d

290. Word Pattern java solutions

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 d

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: pattern = "abba", str = "dog cat cat d

啊啊啊,真的没啥东西好写啊【leetcode】290. Word Pattern

public class Solution { public boolean wordPattern(String pattern, String str) { String[] s=str.split(" "); if(pattern.length()!=s.length) return false; for(int i=0;i<pattern.length();i++) { for(int j=0;j<i;j++) { if((pattern.charAt(i)!=pa