【一天一道LeetCode】#290. Word Pattern
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:字符串模式匹配。给定模板字符串pattern和待匹配字符串str,判断str是否与pattern模式匹配
解题思路:这是一道典型应用hash表求解的题。
利用两个辅助的hash表实现字符串的双向匹配。例如a->dog,dog->a
不允许出现模式中两个不同的字符对应str中同一个单词。
具体思路见代码:
class Solution {
public:
bool wordPattern(string pattern, string str) {
int lenp=pattern.length();
int lens=str.length();
int i = 0;
int j = 0;
unordered_map<string,char> hash;//两个hash表,双向匹配
unordered_map<char,string> hash2;
while(i<lenp&&j<lens){
string temp;
while(j<lens&&str[j]!=' '){//找出str中以空格分隔的字符串
temp+=str[j++];
}
auto iter = hash.find(temp);
auto iter2 = hash2.find(pattern[i]);
if(iter==hash.end()&&iter2==hash2.end()){//如果都不存在
hash[temp] = pattern[i];
hash2[pattern[i]] = temp;//记录双向匹配关系
}
else{
if(hash[temp]==pattern[i]&&hash2[pattern[i]]==temp) {}
else return false;//匹配不上
}
i++;j++;
}
return i>=lenp?j>=lens?true:false:false;//最后需要判断两个字符串是否匹配完
}
};
【一天一道LeetCode】#290. Word Pattern的更多相关文章
- [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 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- [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(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- 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
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
- Java [Leetcode 290]Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- leetcode 290 Word Pattern(map的应用)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [leetcode] 290. Word Pattern (easy)
原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...
随机推荐
- 51 nod 1188 最大公约数之和 V2
1188 最大公约数之和 V2 题目来源: UVA 基准时间限制:2 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个数N,输出小于等于N的所有数,两两之间的最大公约数 ...
- getopt_long函数使用【转】
转自:https://blog.csdn.net/cashey1991/article/details/7942809 平时在写程序时常常需要对命令行参数进行处理,当命令行参数个数较多时,如果按照顺序 ...
- 用Qemu运行/调试arm linux【转】
转自:https://blog.csdn.net/absurd/article/details/78984244 用Qemu运行/调试arm linux,这事情干过好几次了,久了就忘记了,每次都要重新 ...
- Express 配置 https / 443 安全链接
按照教程已配置成功 前一部分内容参照 https://blog.csdn.net/chenyufeng1991/article/details/60340006 前半部分是生成证书文件,关键部 ...
- 实验:利用ASMLib创建ASM磁盘
环境:RHEL 6.5 + Oracle 11.2.0.4 RAC(2 nodes) 目的:在实验环境使用ASMLib配置共享ASM磁盘,虽然我们已经不建议使用ASMLib进行绑盘,但是无奈有客户是这 ...
- CSS :focus 选择器
:focus 选择器用于选取获得焦点的元素. <!DOCTYPE html> <html> <head> <style> input:focus { b ...
- struts2 Action获取表单传值(属性,类))
http://blog.csdn.net/sd0902/article/details/8393157 求大神告知两种方法的不同点 都是写个set方法就行了
- 数据结构之Trie树
1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...
- Spring boot 整合 Mybatis + Thymeleaf开发web(二)
上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...
- python if判断语句&计算
python对缩进要求严格,代码块里的缩进必须一样,可以常用 tab键 表示4个空格 if 条件: 代码块 else: if判断语句如下: 1 print("吃饭,喝水,回家") ...