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<string,char> mapp;
 6         string t;char c;int pos=0;
 7         for(int i=0; i<str.size(); i++,pos++)
 8         {
 9             if(pattern.size()==pos)    return false;
10             t="";
11             c=pattern[pos];
12             while(i<str.size()&&str[i]==‘ ‘)    i++;
13             while(i<str.size()&&str[i]!=‘ ‘)     t+=str[i++];
14             sett[c-‘a‘].insert(t);
15             if(sett[c-‘a‘].size()>1)    return false;
16             if(!mapp[t])    mapp[t]=c;
17             else    if(mapp[t]!=c)    return false;
18         }
19         if(pos!=pattern.size())    return false;
20         return true;
21     }
22 };

AC代码

  用流:

 1 class Solution {
 2 public:
 3     bool wordPattern(string pattern, string str) {
 4         stringstream ss(str);
 5         set<string> sett[26];
 6         map<string,char> mapp;
 7         string t;char c;int pos=0;
 8         while(getline(ss,t,‘ ‘))
 9         {
10             if(pattern.size()==pos)    return false;
11             c=pattern[pos++];
12             sett[c-‘a‘].insert(t);
13             if(sett[c-‘a‘].size()>1)    return false;
14             if(!mapp[t])    mapp[t]=c;
15             if(mapp[t]!=c)    return false;
16         }
17         return pos==pattern.size();
18     }
19 };

AC代码

时间: 2024-10-30 10:16:47

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 --- 模式匹配

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: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(单词模式)(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]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

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.