Word Pattern -- Leetcode
2016-07-02 18:51
405 查看
Given a
find if
Here follow means a full match, such that there is a bijection between a letter in
a non-empty word in
Examples:
pattern =
pattern =
pattern =
pattern =
Notes:
You may assume
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();
}
};
patternand a string
str,
find if
strfollows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in
patternand
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
patterncontains only lowercase letters, and
strcontains
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();
}
};
相关文章推荐
- C#实现DataTable映射成Model的方法(附源码)
- "虚拟路径"..."映射到另一个应用程序,这是不允许的!
- 显示非站点目录及映射网络磁盘路径的图片
- CI映射(加载)数据到view层的方法
- 详解Java的MyBatis框架与Spring框架整合中的映射器注入
- 举例讲解Java的Hibernate框架中的多对一和一对多映射
- 深入解析Java的Hibernate框架中的一对一关联映射
- Java的MyBatis框架中关键的XML字段映射的配置参数详解
- Java的MyBatis框架中XML映射缓存的使用教程
- Java的Hibernate框架中一对多的单向和双向关联映射
- 解析Java的Hibernate框架中的持久化类和映射文件
- python映射列表实例分析
- Python映射拆分操作符用法实例
- Python ORM框架SQLAlchemy学习笔记之映射类使用实例和Session会话介绍
- 浅析php设计模式之数据对象映射模式
- MyBatis高级映射学习教程
- 详解Python中映射类型的内建函数和工厂函数
- Hibernate的基本映射数据类型
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs