Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版
这次是词组字符串和匹配字符串相比较是否一致
请使用map来完成模式统计
class Solution {
public:
bool wordPattern(string pattern, string str) {
map<char,int> mp;
map<string,int> ms;
vector<int> p,s;
int cnt = ;
for(string::size_type i = ; i < pattern.size(); ++i){
if(mp.find(pattern[i]) == mp.end()){
mp[pattern[i]] = cnt++;
p.push_back(mp[pattern[i]]);
}
else p.push_back(mp[pattern[i]]);
}
int a = , b = ;
cnt = ;
while((b = str.find(" ", a)) != string::npos){
string t = str.substr(a, b - a);
if(ms.find(t) == ms.end()){
ms[t] = cnt++;
s.push_back(ms[t]);
}
else s.push_back(ms[t]);
a = b + ;
}
b = str.size();
string t = str.substr(a, b - a);
if(ms.find(t) == ms.end()){
ms[t] = cnt++;
s.push_back(ms[t]);
}
else s.push_back(ms[t]);
if(p.size() != s.size()) return false;
for(vector<int>::size_type i = ; i < s.size(); ++i){
if(s[i] != p[i]) return false;
}
return true;
}
};
Leetcode 290 Word Pattern STL的更多相关文章
- [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
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- 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全栈之路6--正则表达式
正则本身就是一门语言: 正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串,在文本处理方面功能非常强大,也经常用作爬虫,来爬取特定内容,Python本身不支持正则,但是通过导入re模块 ...
- iOS9支付完成无法获取回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id ...
- Maven重复类的解决
1. 设置仓库,我的Settings里设置使用了公司的Nexus <?xml version="1.0"?> <settings xmlns="http ...
- 基于Jquery-ui的自动补全
1.添加CSS和JS引用 <script type="text/javascript" src="javascript/jquery-1.7.min.js" ...
- $ajax引用DOM
- C++变量的左值和右值
变量和文字常量都有存储区,并且有相关的类型. 区别在于变量是寻址的,对于每一个变量,都有两个值与其相关联 1 它的数据值,存储在某个内存地址中.有时这个值也被称为对象的右值 文字常量和变量都可被用作 ...
- JavaScript-遍历数组
遍历数组:依次访问数组中每个元素 for(var i=0; i<arr.length;i++){ arr[i] //当前数组 } <!DOCTYPE html> <html&g ...
- Storm启动流程简介
storm启动流程 storm是一个流行的开源的,分布式实时处理框架,关于storm的基本介绍可以参加这篇官方文档.大致的拓扑结构如图所示: 其中Nimbus是一个后台 ...
- 【Python自动化运维之路Day9】Socket
socket也可以认为是套接字是一种源IP地址和目的IP地址以及源端口号和目的端口号的组合.网络化的应用程序在开始任何通讯之前都必须要创建套接字.就像电话的插口一样,没有它就没办法通讯. socket ...
- Mozilla Firefox 24.0 Beta 5 发布
Mozilla今天将Firefox 24.0 Beta 5版本放到了FTP的release目录,新版开始全面支持OS X 10.7全新的滚动条样式,禁止网站插件运行的功能出现在任务栏左侧,调整了界面U ...