[Leetcode] #290 Word Pattern (map,sstream)
来源:互联网 发布:php注册短信验证 编辑:程序博客网 时间:2024/06/07 03:04
Discription:
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 instr
.
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.
Solution:
bool wordPattern(string pattern, string str) {istringstream strcin(str);string s;vector<string> vs;while (strcin >> s) vs.push_back(s);if (pattern.size() != vs.size()) return false;unordered_map<string, int> s2i;unordered_map<char, int> c2i;for (int i = 0; i < vs.size(); ++i) {if (s2i[vs[i]] != c2i[pattern[i]])return false;s2i[vs[i]] = c2i[pattern[i]] = i + 1;}return true;}
0 0
- [Leetcode] #290 Word Pattern (map,sstream)
- leetcode 290: Word Pattern
- [leetcode 290]Word Pattern
- LeetCode 290 Word Pattern
- [LeetCode 290] Word Pattern
- leetcode 290 Word Pattern
- [leetcode 290] Word Pattern
- LeetCode 290 Word Pattern
- 【LEETCODE】290-Word Pattern
- LeetCode 290:Word Pattern
- leetcode 290 Word Pattern
- leetcode 290: Word Pattern
- 【LeetCode-290】Word Pattern
- LeetCode 290 Word Pattern
- leetcode 290:Word Pattern
- LeetCode[290] Word Pattern
- Leetcode 290 Word Pattern
- [leetcode][290]Word Pattern
- HDU 1574-RP问题
- 【DP】洛谷 P1156 垃圾陷阱
- shiro笔记
- 详解Java8特性之新的日期时间 API
- java中直接输出一个对象
- [Leetcode] #290 Word Pattern (map,sstream)
- I/O模型归纳
- 6条可以让习惯成自然的方法
- 自定义插件
- 用java实现从txt文本文件批量导入数据至数据库
- Boost.Aiso教程 3
- 【搜索】洛谷 P1157 组合的输出
- java中堆、栈以及常量池
- Java运行时异常和非运行时异常