(LeetCode)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.

Credits:

Special thanks to @minglotus6 for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

解题分析:

题目的意思是给出一个字符串 str, 再给出一个模式 pattern,然后要求就是判断给出的字符串是否和给出的模式相匹配,

匹配的话就返回True, 否则返回False.

思路一:

利用hash思想将pattern做索引,str为值存储,与str为索引,pattern为值进行匹配。

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def wordPattern(self, pattern, str):
        words = str.split()
        if len(pattern) != len(words):
            return False
        ptnDict, wordDict = {}, {}
        for ptn, word in zip(pattern, words):
            if ptn not in ptnDict:
                ptnDict[ptn] = word
            if word not in wordDict:
                wordDict[word] = ptn
            if wordDict[word] != ptn or ptnDict[ptn] != word:
                return False
        return True

思路二:

利用位置进行匹配。

使用 index 以及,map,find 函数。

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def wordPattern(self, pattern, str):
        s = pattern
        t = str.split()
        return map(s.find, s) == map(t.index, t)
时间: 2024-10-04 19:03:03

(LeetCode)Word Pattern --- 模式匹配的相关文章

LeetCode Word Pattern II

原题链接在这里:https://leetcode.com/problems/word-pattern-ii/ 题目: 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 substring in

LeetCode Word Pattern (模拟)

题意: 给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern. 思路: 注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同,反过来,一个words[i]对应的也只有一个pattern[i]. 乱搞: 1 class Solution { 2 public: 3 bool wordPattern(string pattern, string str) { 4 set<string> sett[26]; 5 map<

LeetCode:Word Pattern - 字符串模式匹配

1.题目名称 Word Pattern(字符串模式匹配) 2.题目地址 https://leetcode.com/problems/word-pattern/ 3.题目内容 英文:Given a pattern and a string str, find if str follows the same pattern. 中文:给出一组模式(pattern)和一个字符串(str),查看字符串是否与模式匹配 例如: pattern = "abba",str = "dog cat

【一天一道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(单词模式)(istringstream、vector、map)(*)

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

[LeetCode][JavaScript]Word Pattern

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 substring in str. Examples: pattern = "abba", str = "dog c

[LeetCode] 291. Word Pattern II 词语模式 II

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 substring in str. Examples: pattern = "abab", str = "redbluer

LeetCode_290. Word Pattern

290. Word Pattern Easy 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. Example 1: Input: pattern = "abba&q

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.