leetcode 290 Word Pattern(map的应用)
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 dog"should return true. - pattern =
"abba", str ="dog cat cat fish"should return false. - pattern =
"aaaa", str ="dog cat cat dog"should return false. - 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记录就可以。
class Solution {
public:
bool wordPattern(string pattern, string str) {
int len_p=pattern.length();
int len_s=str.length();
vector<string>v();
int id=;
for(int i=;i<len_s;i++){
if(str[i]==' '){
id++;
v.push_back(string());
}
else{
v[id]+=str[i];
}
}
if(len_p!=(id+)){
return false;
}
else{
map<char,string>mp;
map<string,char>mp2;
for(int i=;i<len_p;i++){
if(mp.count(pattern[i])==){
if(mp2.count(v[i])==){
mp[pattern[i]]=v[i];
mp2[v[i]]=pattern[i];
}
else{
return false;
}
}
else{
if(mp[pattern[i]]!=v[i]){
return false;
}
}
}
return true;
}
}
};
leetcode 290 Word Pattern(map的应用)的更多相关文章
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- [LeetCode] 290. Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290. Word Pattern (词语模式)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
- Java [Leetcode 290]Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- [leetcode] 290. Word Pattern (easy)
原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
随机推荐
- 阿里巴巴天猫超市团队招聘java开发工程师
大家好,发个招聘信息:我是阿里巴巴集天猫超市开发团队的同学,我们部门目前在杭州招人,P6岗位,要求至少本科,熟悉java,spring等java开发技术,最好有互联网企业开发经验,感兴趣的可以通过我直 ...
- AMD和Intel的CPU对比
http://www.lotpc.com/yjzs/5825.html 推荐文章:小白看AMD与intel的cpu架构,AMD慢的原因 CPU核心的发展方向是更低的电压.更低的功耗.更先进的制造工艺. ...
- html用jquery获取屏幕宽度与滚动条的关系
当内容高度超过屏幕高度时,获取的屏幕宽度不包括滚动条.即使是浮动,也要显式设置高度,才会全屏. 未超过时,获取的宽度包括滚动条.
- CxImage新手教程,图文并茂
作为一个游戏client程序猿,须要对图像处理有一定的知识. CxImage是C++实现的功能强大的.能处理多种文件格式的图像管理类.它可以简单高速的实现图像的导入.保存.显示和变换. 同一时候又具有 ...
- 1-3:CSS3课程入门之伪类和伪元素
E:target 表示当前的URL片段的元素类型,这个元素必须是E E:disabled 表示不可点击的表单控件 E:enabled 表示可点击的表单控件 E:checked 表示已选中的checkb ...
- pm2 服务崩溃 Error: bind EADDRINUSE
pm2 服务崩溃 Error: bind EADDRINUSE 发布于 1 年前 作者 zhujun24 2444 次浏览 来自 问答 Error: bind EADDRINUSE 0.0.0 ...
- diy文件系统上创建文件的流程
[0]README 0.1) source code are from orange's implemention of a os , and for complete code , please v ...
- oracle数据库表格操作
create table dept--创建表格( deptno number(2) primary key, dname varchar2(9) check(dname=Upper(dname)), ...
- 关于海康视频采集卡的简介---基于pci的插潮采集卡
vga 640x480 qvga vga的1/4,宽高分别是vga的一半 (1)采集类型 海康威视 DS-2CE16A2P-IT3P 700TVL 1/3" DIS ICR 红外防水筒型摄像 ...
- 多媒体开发之---如何确定slice_header slice_type 的位置
引用网友的问答:我找到0x000001 NAL的开头了,请问如何确定slice head的位置,继而得出slice_type呢?Nal unit后紧跟的就是slice head吗?标准里的循环让人看得 ...