LeetCode 290 Word Pattern
Problem:
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.
Summary:
判断所给字符串str中单词的形式与pattern中是否相符。
Solution:
以key为pattern中的char,value为str中的单词构成Hash Table,每加进一个新单词,判断原来若存在相同key的单词是否与新单词相同。
最终需排除不同key,value相同的情况。
class Solution {
public:
bool wordPattern(string pattern, string str) {
vector<string> s;
unordered_map<char, string> m;
int flag = ;
for (int i = ; i < str.size(); i++) {
if (str[i] == ' ') {
s.push_back(str.substr(flag, i - flag));
flag = i + ;
}
}
s.push_back(str.substr(flag, str.size() - flag));
if (pattern.size() != s.size()) {
return false;
}
for (int i = ; i < pattern.size(); i++) {
if (m[pattern[i]] == "") {
m[pattern[i]] = s[i];
}
else if (m[pattern[i]] != s[i]){
return false;
}
}
for (unordered_map<char, string>::iterator i = m.begin(); i != m.end(); i++) {
for (unordered_map<char, string>::iterator j = m.begin(); j != m.end(); j++) {
if (i->second == j->second && i->first != j->first) {
return false;
}
}
}
return true;
}
};
LeetCode 290 Word Pattern的更多相关文章
- [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 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- 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 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(map的应用)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [leetcode] 290. Word Pattern (easy)
原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...
随机推荐
- Python进制转换
一 内置函数 bin().oct().hex()的返回值均为字符串,且分别带有0b.0o.0x前缀. 实例 统计二进制数里1的个数 def countBits(n): return bin(n).co ...
- 关于input的file框onchange事件触发一次失效的新的解决方法
在google了众多方法后,网上有这么几种方法: 1.替换掉原来的input框 2.remove原来的input框,然后在添加进新的一样的input框 但是不知道为什么非常不幸的是,怎么弄我都弄不出. ...
- window.hostory(浏览器的历史记录)
浏览器会对同一个窗口(选项卡)中访问的网页进行记录,不管我们是通过以下哪种方式改变网页,浏览器都会把改变后的网页记录下来,以便通过浏览器的前进和后退按钮,能够快速的切换到已经访问过的网页: 1)直接 ...
- POJ 3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- project.pbxproj 的merge问题
基于xcode8.0 1.project.pbxproj 的结构 内部文件{archiveVersion=1 ; classes={};objectVersion=46;objects={};root ...
- 使用Xmanager访问CentOS远程桌面
最近在搞Qemu虚拟机相关的项目,需要用到Linux的桌面系统,用Xmanager连接CentOS桌面最方便了. Linux端:CentOS release 6.8 (Final) Windows端: ...
- Linux学习之CentOS(二十一)--Linux系统启动详解
在这篇随笔里面将对Linux系统的启动进行一个详细的解释!我的实验机器是CentOS6.4,当然对于现有的Linux发行版本,其系统的启动基本上都是一样的! 首先我们来看下Linux系统启动的几个 ...
- Python学习目录
日期 科目 状态 知识点 2016.12.10 subprocess OK 执行外部shell命令 2016.12.11 threading ? 线程池,Lock,适合IO密集型应用,信号量? 201 ...
- Event Loop个人理解
javascript引擎单线程程序,其执行同步操作会按顺序向下执行,执行异步操作则使用事件循环模型. js引擎线程遇到异步操作,会将异步操作交给对应的观察者, 异步操作包括: dom事件 click, ...
- DateUtils 学习记录1
开发过程中很多时候都需要处理各种各样的日期..有些项目可能还会有自己的DateUtil.... 其实apache commons lang3有一个很好用的日期处理工具类,叫DateUtils... 基 ...