【一天一道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 * @ ...
随机推荐
- 2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree
Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A learne ...
- diango-团队介绍
1.使用django-admin startproject show创建项目,并使用python manage.py startapp team_show创建应用 2.进行相关的配置 3.代码的实现
- string转换为guid类型 split
string str = "{"+context.Request["ID"]+"}"; KpiUser.ID = new Guid(str) ...
- Redis数据库之概念与创建服务
概念 Remote Dictionary Server key-value 数据库存储系统,数据结构服务器. 键是Stri ...
- Mysql根据一个基库生成其他库与其不同的库升级脚本
今天研究了一下不同数据库之间如何做同步.弄了一个升级工具类,希望以后还能有所帮助. public class UpgradeDataBase { public static void main(Str ...
- js数组排序,支持正反排序以及多维度排序
工作中遇到js数组排序问题,数组中存储的都是对象,于是就百度了下,利用别人的代码进行修改,最终完成可以倒序.反序,可以进行多维度排序的功能源码如下: /** * js数组排序 支持数字和字符串 * @ ...
- Android Studio精彩案例(五)《JSMS短信验证码功能实现》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 很多应用刚打开的时候,让我们输入手机号,通过短信验证码来登录该应用.那么,这个场景是怎么实现的呢?其实是很多开放平台提供了短信验证功能 ...
- 基于hadoop的BI架构
BI系统,是企业利用数据驱动运营的一个典型系统.BI系统通过发掘企业运行过程中的数据,发现企业的潜在风险.为企业的各项决策提供数据支撑. 传统的BI系统通常构建于关系型数据库之上.随着企业业务量的增大 ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- RxJava(11-线程调度Scheduler)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51821940 本文出自:[openXu的博客] 目录: 使用示例 subscribeOn原理 ...