题目:

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.

思路:考察的是字符串模式匹配,可以用map来解决,先将str分割放到一个vector<string>中,再将pattern分割一起放入map<char,string>中,遍历判断是否是对应的键或值即可。

代码:

class Solution {
public:
bool wordPattern(string pattern, string str) {
stringstream ss(str);
string s;
vector<string> strVec;
while(ss >> s)
{
strVec.push_back(s);
} if(strVec.size() != pattern.size())
return false;
map<char, string> hash;
int i = ;
for(auto c : pattern)
{
if(hash.count(c))
{
if(hash[c] != strVec[i])
return false;
}
else
{
for(auto p : hash)
{
if(p.second == strVec[i])
return false;
}
hash[c] = strVec[i];
}
++i;
}
return true;
}
};

[LeetCode290]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. Word Pattern | & II

    Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...

  5. 291. Word Pattern II

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

  6. leetcode面试准备: Word Pattern

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

  7. Word Pattern

    ​package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** *  * @au ...

  8. Word Pattern II 解答

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

  9. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

随机推荐

  1. Ubuntu14.04搭建android开发环境

    一 下载ADT 官方下载地址:http://developer.android.com/sdk/index.html(须要FQ或者改动host) 二 解压 1 使用终端将下载的文件解压当前文件夹下: ...

  2. zoj 2972 - Hurdles of 110m

    题目:110米栏,运动员能够用三种状态跑,1状态耗体力且跑得快,2状态不消耗体力,3状态恢复体力且跑得慢. 体力上限是M,且初始满体力,如今想知到最小的时间跑全然程. 分析:dp,全然背包.题目是一个 ...

  3. Linux 挂载NTFS文件系统

    步骤如下: 1.安装ntfs-3g包 [root@CS-1 pub]# yum install ntfs-3g 2.创建挂载目录 [root@CS-1 pub]# mkdir /data 3.挂载NT ...

  4. curl的封装

    首先要搭建一个httpserver,这里採用tomcat6为例: 过程:新建一个Servlet,并使用tomcat的默认port号8080监听,最后写一个jsp来測试能否够訪问该server 1)新建 ...

  5. dedecms 获取描述信息限制字数

    对于我刚刚刚开始对于获取到了描述的信息,但是有些字数简直是太多了,显示的样式不好看,所以我就希望限制字数,所以我来告诉你们获取描述信息限制字数的语法吧[field:description functi ...

  6. TF卡分区

    http://bbs.gfan.com/android-5176910-1-1.html http://www.miui.com/thread-2302600-1-1.html http://www. ...

  7. MVAPI第一个版本架构图

    MVAPI采用矢量与栅格结合的方式进行移动地图的显示. 进过几个月,目前终于可以完成基本的地图显示及操作功能.还有待实现的是各种性能及效果优化.3D地物等. 发一个1.0的架构图留存一下.(虽然目前还 ...

  8. 【转】真正的Acmer

    上海交大 戴文渊 大牛写的东西,建议大家看看 yiyiyi4321 2007-07-10 13:49:30.0 http://dwyak.spaces.live.com/?_c11_BlogPart_ ...

  9. Failed to load libGL.so问题解决

    Ubuntu 14.04下启动模拟设备Android 4.2.2的时候报错: failed to load libgl.so 先用locate 命令定位libGL库, 然后加入�一个链接就可以: de ...

  10. WampServer:轻松配置Wordpress安装环境

    WordPress运行环境 PHP 5.2.4 或更新版本(不支持第三方推出的“PHP 6.0”) MySQL 5.0 或更新版本 Apache mod_rewrite 模块(可选,用于支持“固定链接 ...