[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的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
随机推荐
- 知识网之C++总结
米老师常说的一句话:构造知识网. 立即要考试了.就让我们构造一下属于C++的知识网.首先从总体上了解C++: 从图中能够了解到,主要有五部分.而当我们和之前的知识联系的话,也就剩下模板和运算符重载以及 ...
- UVa-Palindromes
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- POJ题目分类【实在是不知道哪个是原创了】
原地址:http://blog.csdn.net/liuqiyao_01/article/details/8477801 初期:一.基本算法: (1)枚举. (poj1753,poj2965) ...
- c++一些语法模板
函数模板特 template <class T> int compare(T v1,T v2) { if(v1<v2) return -1; else if(v1>v2) re ...
- 在word 中复选框划勾或叉的方法
输入大写字母R.大写字母Q ,然后将字体改为Wingdings 2, 就分离得到带框的勾和叉.
- hdu3001(状压dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:n 个城市已经 m 条路 以及对应路费 c,要求遍历所有城市最少的路费,每个城市不能超过2 ...
- Remote Desktop Organizer – 管理组织远程桌面 - 小众软件
http://www.appinn.com/remote-desktop-organizer/
- SurfaceView的一个小应用:开发示波器
SurfaceView与普通View还有一个重要区别:View的绘图必须在UI线程中进行,但SurfaceView不存在这个问题,因为它是由SurfaceHolder来完成的.所以对于View组件,如 ...
- Linux lamp环境编译安装
1.安装准备: 1)httpd-2.0.52.tar.gz 2)mysql-4.1.12.tar.gz 3)libxml2-2.6.27.tar 4)freetype-2.1.10.tar 5)gd- ...
- 所有javax包
所有jar包 > javax javax 下载 javax jar 包 本站下载镜像: javax-jmi-model.jar.zip javax-jmi-reflect.jar.zip jav ...