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 ...
随机推荐
- 80C51单片机指令的取指、执行时序
80C51单片机指令的取指.执行时序 现按4类指令介绍CPU时序.因为CPU工作的过程就是取指令与执行指令的过程,所以CPU必须先取出指令,然后才能执行指令. 1.双字节单周期指令 由于双字节单周期指 ...
- SQL语句Not IN优化方案
总结网友们在CSDN社区上对于not in的优化策略,整理如下,备查. select * from emp where emp_no not in (select emp_no from emp ...
- Android 使用剪贴板传递简单数据及复杂数据的方法
传递数据的场景在于不同页面之间跳转,需要携带数据:简单数据值指的是String, int等数据, 复杂数据指的是类 1. 使用剪贴板传递简单数据方法: 第一个页面里面放数据操作如下: Clipbo ...
- 从shell(终端)中退出python
从shell(终端)中退出python: 1.输入命令行:$ exit() 2.快捷键: ctrl+Z
- java第五次课堂笔记
- 【homework #1】第一次作业被虐感受
当大二暑假结束,我发现我还是没有熟练掌握一门编程语言时,我就知道苦日子要来了. 这不,才开学第三周,就已经被虐的体无完肤了.连编译课用C语言写的词法分析,都要写很久.Debug很久才能写出来,更别提大 ...
- SSM框架 springMVC对静态资源访问的处理
https://my.oschina.net/hnqingping1255/blog/415575 错误信息 [org.springframework.web.servlet.PageNotFound ...
- C#控制台程序读取项目中文件路径
//使用appdomain获取当前应用程序集的执行目录 string dir = AppDomain.CurrentDomain.BaseDirectory; //使用path获取当前应用程序集的执行 ...
- 网络流 24 题汇总(LOJ 上只有 22 题???)
太裸的我就不放代码了...(黑体字序号的题表示值得注意) 1.搭配飞行员 [LOJ#6000] 二分图最大匹配. 2.太空飞行计划 [LOJ#6001] 最小割常规套路.输出方案.(注:这题换行符要用 ...
- http content-type总结
content-type 媒体类型,即MIME类型,包括媒体格式和编码两部分 例如:Content-Type:text/html;charset:utf-8 常见的媒体格式类型如下: text/htm ...