题意:

  给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern。

思路:

  注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同,反过来,一个words[i]对应的也只有一个pattern[i]。

  乱搞:

 class Solution {
public:
bool wordPattern(string pattern, string str) {
set<string> sett[];
map<string,char> mapp;
string t;char c;int pos=;
for(int i=; i<str.size(); i++,pos++)
{
if(pattern.size()==pos) return false;
t="";
c=pattern[pos];
while(i<str.size()&&str[i]==' ') i++;
while(i<str.size()&&str[i]!=' ') t+=str[i++];
sett[c-'a'].insert(t);
if(sett[c-'a'].size()>) return false;
if(!mapp[t]) mapp[t]=c;
else if(mapp[t]!=c) return false;
}
if(pos!=pattern.size()) return false;
return true;
}
};

AC代码

  用流:

 class Solution {
public:
bool wordPattern(string pattern, string str) {
stringstream ss(str);
set<string> sett[];
map<string,char> mapp;
string t;char c;int pos=;
while(getline(ss,t,' '))
{
if(pattern.size()==pos) return false;
c=pattern[pos++];
sett[c-'a'].insert(t);
if(sett[c-'a'].size()>) return false;
if(!mapp[t]) mapp[t]=c;
if(mapp[t]!=c) return false;
}
return pos==pattern.size();
}
};

AC代码

LeetCode Word Pattern (模拟)的更多相关文章

  1. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  3. [LeetCode] Word Pattern

    Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...

  4. Leetcode: Word Pattern II

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  5. Leetcode solution 291: Word Pattern II

    Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...

  6. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

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

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

  8. [LeetCode] 290. Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  9. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

随机推荐

  1. 第七章 企业项目开发--本地缓存guava cache

    1.在实际项目开发中,会使用到很多缓存技术,而且数据库的设计一般也会依赖于有缓存的情况下设计. 常用的缓存分两种:本地缓存和分布式缓存. 常用的本地缓存是guava cache,本章主要介绍guava ...

  2. Notepad++ 配置java编译环境

    仅限于学习java或小的java程序使用.正常写代码还是eclipse吧 ---------------------分割线----------------------------- 1.配置JDK环境 ...

  3. Jquery.Page.js 分页插件的使用

    1.简单直接贴代码 需要引用以下样式和脚本 <link href="~/Scripts/Page/pager.css" rel="stylesheet" ...

  4. qml 相关的博客

    http://qt-project.org/wiki/SpaceAppsChallengeResources http://www.ics.com/blog https://www.ics.com/d ...

  5. SG函数 模板

    int get_SG(int x) { ) return SG[x]; ]={}; ;i<=n;i++) ) v[get_SG(x-s[i])]=; int i; ;v[i];i++); SG[ ...

  6. Android Phonebook编写联系人UI加载及联系人保存流程(六)

    2014-01-07 11:18:08 将百度空间里的东西移过来. 1. Save contact 我们前面已经写了四篇文章,做了大量的铺垫,总算到了这一步,见证奇迹的时刻终于到了. 用户添加了所有需 ...

  7. bzoj 1185 旋转卡壳 最小矩形覆盖

    题目大意 就是求一个最小矩形覆盖,逆时针输出其上面的点 这里可以看出,那个最小的矩形覆盖必然有一条边经过其中凸包上的两个点,另外三条边必然至少经过其中一个点,而这样的每一个点逆时针走一遍都满足单调性 ...

  8. java中利用JFrame创建窗体 【转】

    1. 一个简单的swing public class Test(){ public static void main(String[] args){ JFrame frame = new JFrame ...

  9. 记录一些容易忘记的属性 -- UILabel

    一:UILabel lbl.alpha=0.f;    lbl 透明,会影响子视图的显示 lbl.backgroundColor=[UIColor clearColor]; lbl 背景色透明,子视图 ...

  10. SharePoint重置密码功能Demo

    博客地址 http://blog.csdn.net/foxdave 本文将说明一个简单的重置SharePoint用户密码(NTLM Windows认证)的功能如何实现 重置密码功能,实际上就是重置域用 ...