[LeetCode290]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 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来解决,先将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的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- Word Pattern
package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** * * @au ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
随机推荐
- 在后台运行erlang;在需要时连回交互模式
* 1. 启动后台运行的erlang环境 按以下命令: erl -detached -name a@127.0.0.1 注意,-name的值必须是xxxx@ip的形式.其中xxxx是英文名,ip必须是 ...
- ASA基本配置
拓扑如下: ASA5520# show running-config : Saved:ASA Version 8.0(2) !hostname ASA5520enable password 2KFQn ...
- hdu4487(概率dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4487 题意:开始位置在0,每一步可以向右向左或者不动,问走了n步后,路径中能到达最右的期望. 分析:d ...
- hdu2870(dp求最大子矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870 分析:分别转换成'a','b','c'三种来求,其实就跟hdu1505一样了... #inclu ...
- inkscape - 百度百科
http://wapbaike.baidu.com/view/1267893.htm?ssid=0&from=844b&uid=3151E6C0905477A13653132D762B ...
- 9个杀手级 JVM 编程语言
9个杀手级 JVM 编程语言 Java虚拟机已经不再是仅仅局限在 Java 了,很多语言提供了脚本转换,可以让其他的程序在java虚拟机上运行,这样能够让更多的开发者能够依靠JVM在Java平台上大有 ...
- 有向无环图(DAG)的最小路径覆盖
DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如 ...
- httpcomponents-client-4.3.x DOC
Chapter 1. Fundamentals Prev Next Chapter 1. Fundamentals 1.1. Request execution The most essent ...
- 屌丝程序猿赚钱之道之taobao 2
续上篇,之前写的案例,都是比較0基础的. 案例4: 代写情书.软文.论文等等. 这是我一个同学的真实故事. 我隔壁寝室的小王平时没事就爱谢谢博客.逛逛论坛.大二的时候接触了威客网,開始在网上 ...
- VMware vSphere 服务器虚拟化之二十六 桌面虚拟化之View Persona Management
VMware vSphere 服务器虚拟化之二十六 桌面虚拟化之View Persona Management 实验失败告终,启动VMware View Persona Management服务报10 ...