[LeetCode] Word Pattern 词语模式
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
.
Example 1:
- Input: pattern =
"abba"
, str ="dog cat cat dog"
- Output: true
Example 2:
- Input:pattern =
"abba"
, str ="dog cat cat fish"
- Output: false
Example 3:
- Input: pattern =
"aaaa"
, str ="dog cat cat dog"
- Output: false
Example 4:
- Input: pattern =
"abba"
, str ="dog dog dog dog"
- Output: false
Notes:
You may assume pattern
contains only lowercase letters, and str
contains lowercase letters separated by a single space.
Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.
这道题给我们一个模式字符串,又给我们一个单词字符串,让我们求单词字符串中单词出现的规律是否符合模式字符串中的规律。那么首先想到就是用 HashMap 来做,建立模式字符串中每个字符和单词字符串每个单词之间的映射,而且这种映射必须是一对一关系的,不能 'a' 和 'b' 同时对应 'dog',也不能 'a' 同时对应到 'dog' 和 'cat',所以我们在碰到一个新字符时,首先检查其是否在 HashMap 中出现,若出现,其映射的单词若不是此时对应的单词,则返回 false。如果没有在 HashMap 中出现,我们还要遍历一遍 HashMap,看新遇到的单词是否已经是其中的映射,若已经有其他映射,直接返回 false,如果没有,再跟新遇到的字符建立映射。最后循环退出后,要检查此时的 i 是否和 n 相同,这是检查一对一映射的最后一步,因为当 str 中的单词处理完了之后,pattern 中就不能有多余的字符了,参见代码如下:
解法一:
- class Solution {
- public:
- bool wordPattern(string pattern, string str) {
- unordered_map<char, string> m;
- istringstream in(str);
- int i = , n = pattern.size();
- for (string word; in >> word; ++i) {
- if (i >= n) continue;
- if (m.count(pattern[i])) {
- if (m[pattern[i]] != word) return false;
- } else {
- for (auto a : m) {
- if (a.second == word) return false;
- }
- m[pattern[i]] = word;
- }
- }
- return i == n;
- }
- };
当然这道题也可以用两个 HashMap 来完成,分别将字符和单词都映射到当前的位置加1,注意这里需要加1就是为了避免默认映射值0,因为 C++ 中的 HashMap 的机制是若访问一个不存在的 key 值,会默认建立一个映射值为0的映射。那么我们在遇到新字符和单词时,首先看 i 是否已经是 n 了,若相等了,说明此时 pattern 中的字符已经用完了,而 str 中还有多余的单词,这样是无法建立一对一映射的,直接返回 false。还有当两个 HashMap 的映射值不相同时也返回 false,否则我们同时建立单词和 pattern 字符和当前位置加1之间的映射,循环推出后还是要检测 i 是否和 n 相等,参见代码如下:
解法二:
- class Solution {
- public:
- bool wordPattern(string pattern, string str) {
- unordered_map<char, int> m1;
- unordered_map<string, int> m2;
- istringstream in(str);
- int i = , n = pattern.size();
- for (string word; in >> word; ++i) {
- if (i == n || m1[pattern[i]] != m2[word]) return false;
- m1[pattern[i]] = m2[word] = i + ;
- }
- return i == n;
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/290
类似题目:
参考资料:
https://leetcode.com/problems/word-pattern/
https://leetcode.com/problems/word-pattern/discuss/73402/8-lines-simple-Java
https://leetcode.com/problems/word-pattern/discuss/73409/Short-C%2B%2B-read-words-on-the-fly
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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] Word Pattern 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 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
- 290 Word Pattern 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...
- [LeetCode] 132 Pattern 132模式
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- LeetCode Word Pattern (模拟)
题意: 给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern. 思路: 注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同, ...
随机推荐
- 多项目并行开发如何做到快速切换——sublime Text3
sublime text有一个很人性化的功能,就是打开窗口的时候,它会把上一次关闭时的编辑器工作区状态完全复原(不论文件是否已经保存). 只有一个项目的时候,这个功能非常方便,可以保证重启电脑后cod ...
- [JavaEE笔记]Cookie
引言 由于 Http 是一种无状态的协议,服务器单从网络连接上无从知道客户身份. 会话跟踪是 Web 程序中常用的技术,用来跟踪用户的整个会话.常用会话跟踪技术是 Cookie 与 Session. ...
- php内核分析(二)-ZTS和zend_try
这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux ZTS 我们会看到文章中有很多地方是: #ifdef ZTS # define CG(v) ZEND_TSRMG(comp ...
- Nginx最大客户连接数算法一些遐想
Nginx最大客户连接数算法一些遐想 现在很多互联网公司都在使用nginx,并且替换掉以前的Apache,nginx的优点就不说了,浅聊两句nginx的某些配置参数,找到这些参数设置的目的和关联性,并 ...
- JavaScript jQuery 中定义数组与操作及jquery数组操作
首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...
- 浅谈Static关键字
1.使用static关键字声明的属性为全局属性 未使用static关键字指定city之前,如果需要将Tom,Jack,Mary三人的城市均改成Beijing,需要再次声明三次对象的city为Beiji ...
- ASP.NET MVC企业级实战目录
电子书样稿 (关注最新进度,请加QQ群:161436236) ASP.NET MVC企业实战第1章 MVC开发前奏.pdf ASP.NET MVC企业实战第10章 站内搜索.pdf 已经好长一段时间没 ...
- Struts2入门(二)——配置拦截器
一.前言 之前便了解过,Struts 2的核心控制器是一个Filter过滤器,负责拦截所有的用户请求,当用户请求发送过来时,会去检测struts.xml是否存在这个action,如果存在,服务器便会自 ...
- SpringMVC类型转换器、属性编辑器
对于MVC框架,参数绑定一直觉得是很神奇很方便的一个东西,在参数绑定的过程中利用了属性编辑器.类型转换器 参数绑定流程 参数绑定:把请求中的数据,转化成指定类型的对象,交给处理请求的方法 请求进入到D ...
- (转)配置Log4j(很详细)
来自:http://blog.csdn.net/yttcjj/article/details/37957317 Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存 ...