[LeetCode] 290. 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] 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] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- 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 ...
- 290 Word Pattern 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
- [leetcode] 290. Word Pattern (easy)
原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
随机推荐
- javascript判断mp3是否播放完
javascript判断mp3是否播放完 var audio=document.getElementById('audio'); if(audio){ audio.loop = false; audi ...
- Logstash:处理多个input
Logstash:处理多个input Logstash的整个pipleline分为三个部分: input插件:提取数据. 这可以来自日志文件,TCP或UDP侦听器,若干协议特定插件(如syslog或I ...
- 阿里云(百度云)Linux系统挂载磁盘
阿里云(百度云)Linux系统挂载磁盘
- Winform中设置ZedGraph的曲线符号Symbol以及对应关系
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- codeforces #579(div3)
codeforces #579(div3) A. Circle of Students 题意: 给定一个n个学生的编号,学生编号1~n,如果他们能够在不改变顺序的情况下按编号(无论是正序还是逆序,但不 ...
- Lucene搜索核心代码TermInfosReader
TermInfosReader类是Lucene搜索的核心代码,所有的搜索最终都是落到通过term查询,TermInfosReader里定义了支持的基础的term查询功能. 前置知识: 词元字典文件(t ...
- mssql 导出作业,导出表,导出存储过程等
1.mssql 导出作业 1.选中sql server 代理-作业 2.点击F7 显示对象资源管理器详细信息,3.按住ctrol 选中 右键编写作业脚本到就可以了 2.mssql 导出表和存储过程等 ...
- inndo 表与存储逻辑_1
------------------------------------------2015-03-03--------------------------------------- 表 : inno ...
- nrm : 无法加载文件 C:\Users\TANG\AppData\Roaming\npm\nrm.ps1,因为在此系统上禁止运行脚本。
1.win+s 搜索powershell 以管理身份运行 2.使用set-ExecutionPolicy RemoteSigned命令将计算机上的执行策略更改为 RemoteSigned,输入Y确定 ...
- Linux创建高级用户并删除
Linux创建高级用户并删除 常见window系统可以创建许多用户,但是linux也可以创建许多用户. 方法比window方便简单. (1)添加一个普通用户 :nangong(名字自己取) usera ...