Word Pattern -- Leetcode
来源:互联网 发布:淘宝网商城儿童鞋 编辑:程序博客网 时间:2024/06/01 10:24
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.
题目可以理解为
存在两个集合,找出集合元素之间的是否存在一一对应的映射关系。
class Solution {public: bool wordPattern(string pattern, string str) { map<string, int> map_p; map<string, int> map_s; istringstream in(str); int i = 0; for (string word_s; pattern.size() && in >> word_s ; ++i) { const string word_p(1, pattern[i]); if (map_s[word_s] != map_p[word_p]) return false; map_p[word_p] = map_s[word_s] = i + 1; } return i == pattern.size() && in.eof(); }};
0 0
- leetcode | Word Pattern
- 【leetcode】290. Word Pattern
- leetcode 290: Word Pattern
- [Leetcode]Word Pattern
- [leetcode 290]Word Pattern
- LeetCode 290 Word Pattern
- 【leetcode】290. Word Pattern
- [LeetCode 290] Word Pattern
- leetcode 290 Word Pattern
- LeetCode 2 Word Pattern
- leetcode word-pattern
- leetcode: Word Pattern
- leetcode:Word Pattern
- [Leetcode]Word Pattern
- LeetCode Word Pattern
- LeetCode-Word Pattern
- LeetCode:Word Pattern
- leetcode之Word Pattern
- HTTP方法详解
- 四子连棋
- <LeetCode OJ> 371. Sum of Two Integers
- mysql 数据库乱码配置文件设置
- 倒水
- Word Pattern -- Leetcode
- 魔版
- 最小生成树
- 51nod最长公共子序列
- css样式重置
- car的旅行路线
- Xcode报错
- 深入理解Java中的final关键字
- shell 脚本中的$?是什么意思?