Word Pattern - LeetCode
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.
思路:使用两个map记录是否pattern和str匹配。
注意,map的键值这里不能为0,因为为0时map默认是访问了未创建过的键,因此要从1开始。
此外,for循环里真的可以实现很多东西,用好了会让代码很精简。
class Solution {
public:
bool wordPattern(string pattern, string str) {
map<char, int> dict1;
map<string, int> dict2;
stringstream ss(str);
int i = , n = pattern.size();
for (string word; ss >> word; i++)
{
if (i == n || dict1[pattern[i]] != dict2[word])
return false;
dict1[pattern[i]] = dict2[word] = i + ;
}
return i == n;
}
};
Word Pattern - LeetCode的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- Leetcode solution 291: Word Pattern II
Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...
- [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 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] 291. Word Pattern II 词语模式 II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290. 单词规律(Word Pattern) 41
290. 单词规律 290. Word Pattern 题目描述 给定一种规律 pattern 和一个字符串 str,判断 str 是否遵循相同的规律. 这里的 遵循 指完全匹配,例如,pattern ...
随机推荐
- Sql日期时间格式转换(转 子夜.)
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- 3 View - 错误视图函数
1.定义视图 本质就是一个函数 视图的参数 一个HttpRequest实例 通过正则表达式组获取的位置参数 通过正则表达式组获得的关键字参数 在应用目录下默认有views.py文件,一般视图都定义在这 ...
- sql server 不可见字符处理 总结
前言 问题描述:在表列里有肉眼不可见字符,导致一些更新或插入失败. 几年前第一次碰见这种问题是在读取考勤机人员信息时碰见的,折腾了一点时间,现在又碰到了还有点新发现就顺便一起记录下. 如下图所示 go ...
- linux学习(四) -- supervisor守护进程
supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启. 1.安装 apt-get install ...
- Pre 自动换行和手动换行
pre { white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre ...
- 接口测试之post和get的区别
post和get都可以给服务器发送请求,在做接口测试的时候,我发现有些时候某些功能的接口文档中是用post请求发送的, 但是只要接口一致参数一致用post也能发送请求,并且获取到的返回也是正确的. 那 ...
- 《HTTP协议详解》读书笔记---请求篇之响应状态码
在接收和解释请求消息后,服务器返回一个http响应消息.它也分为3个部分:状态行.消息报头.响应正文,格式如下: HTTP-VersionStatus-CodeReason-PhraseCRLF(CR ...
- Leetcode 599.两个列表的最小索引总和
两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答 ...
- docker安装中附带安装的其他软件
aufs-tools: Tools to manage aufs filesystems. aufs的全称是advanced multi-layered unification filesystem, ...
- maven学习(十二)——maven聚合与继承实战
聚合与继承实战 创建四个Maven项目,如下图所示: