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 * @ ...
随机推荐
- BZOJ 4742: [Usaco2016 Dec]Team Building
4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved: 16[Su ...
- IIS配置MP3/MP4/OGG/flv等资源文件访问
配置过程参考:http://www.cnblogs.com/EasonJim/p/4752399.html 以下包含了mp4的mime类型: 323 text/h323 acx application ...
- fedora22,fedora24最简单的安装virtaulbox的方法
fedora为什么不好用呢? 1.因为很多软件没有预先安装,新手安装时,就无从下手了. 2.版本更新太快,有老手提供了解决方案,但是版本更新了,新手按照步骤来,就不能配置成功! 不抱怨了. 安装vir ...
- Python批量扫描服务器指定端口状态
闲来无事用Python写了一个简陋的端口扫描脚本,其简单的逻辑如下: 1. python DetectHostPort.py iplist.txt(存放着需要扫描的IP地址列表的文本,每行一个地址) ...
- 我的Debian KDE常用软件记录
1.看图 digiKam 2.音乐 Amarok
- [01] cocos2d-x开发环境搭建
cocos2d-x 是跨平台的游戏开发引擎,支持的平台有 ios , android , windows phone , web , tizen,windows等. 先来搭建开发环境,一般我们开发游戏 ...
- iOS 中的 promise 模式
1.概述 异步编程 App 开发中用得非常频繁,但异步请求后的操作却比较麻烦.Promise 就是解决这一问题的编程模型.其适用于 延迟(deferred) 计算和 异步(asynchronous) ...
- webSphere内存溢出
有一个做了很长时间的项目,是用websphere做生产环境的,可是一旦加载的项目过多,webSphere就很傲娇的内存溢出,这是一个折腾了公司里某个前辈很久很久的问题,因为是测试版,所以各种官方文档说 ...
- 关于HTML5本地缓存技术LocalStorage 本地存储 和 SessionStorage
如果你想在用户访问的时候记录或者记住他们的行为,你会想到的是什么,cookie 和session.但今天告诉你还有两种或者说是1种吧 那就是html5的 LocalStorage 本地存储和 Sess ...
- R语言画云字图
install.packages('wordcloud') library(wordcloud) colors=c('red','blue','green','yellow','purple') da ...