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

类似题目:

Isomorphic Strings

Word Pattern II

参考资料:

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 词语模式的更多相关文章

  1. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

  3. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  4. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  5. LeetCode 290. Word Pattern (词语模式)

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  6. 290 Word Pattern 单词模式

    给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...

  7. Leetcode 290 Word Pattern STL

    Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...

  8. [leetcode] 290. Word Pattern (easy)

    原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...

  9. LeetCode 290 Word Pattern

    Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

随机推荐

  1. CentOS7安装Oracle 11g数据库

    转载:https://blog.csdn.net/lia17/article/details/82256565 rpm -ivh --force --nodeps *.rpm 强制装 rpm依赖包下载 ...

  2. Vue.js 源码分析(十五) 指令篇 v-bind指令详解

    指令是Vue.js模板中最常用的一项功能,它带有前缀v-,比如上面说的v-if.v-html.v-pre等.指令的主要职责就是当其表达式的值改变时,相应的将某些行为应用到DOM上,先介绍v-bind指 ...

  3. ELK 日志平台 For Windows

    一.Logstash 安装 1. 下载最新版本的logstash:  https://www.elastic.co/fr/downloads/logstash 下载zip格式的压缩包. 然后解压缩放到 ...

  4. [笔记] NuGet Warning NU5100 处理

    问题描述 在将 .NET 项目编译成 NUGET 包时,如果项目引用了其它 dll 文件,则可能出现这个警告,如果不处理,Nuget 包中可能无法正确引用所添加的 dll . 处理方式 在项目 .cs ...

  5. asp.net 获取当前,相对,绝对路径

    一.C#获取当前路径的方法: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -获取模块的完整路径. 2. ...

  6. mssql like 优化

    SqlServer中like 的查询一般我们都不推荐,但是当数据库某个字段的值是用分隔符区分的多个链接字符,比如,12,11,23等这样的类型.可能我们需要判断是否包含12. 这个时候我们想到的当然是 ...

  7. Vue.js项目实战-多语种网站(租车)

    首先来看一下网站效果,想写这个项目的读者可以自行下载哦,地址:https://github.com/Stray-Kite/Car: 在这个项目中,我们主要是为了学习语种切换,也就是右上角的 中文/En ...

  8. VS2010 报错该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失

    尤其代码是从linux平台复制过来: 报错如图: 更有甚者基本函数都报错: 当下检查发现if else break case等基本函数并无问题时,报错行数明显不一致等一定要注意文档编码格式, 最简单的 ...

  9. SSH开发模式——Struts2进阶

    在之前我有写过关于struts2框架的博客,好像是写了三篇,但是之前写的内容仅仅是struts2的一些基础知识而已,struts2还有很多有趣的内容等待着我们去发掘.我准备再写几篇关于struts2的 ...

  10. linux下各文件夹的结构说明及用途介绍:Linux目录结构介绍

    linux下各文件夹的结构说明及用途介绍: /bin:二进制可执行命令. /dev:设备特殊文件. /etc:系统管理和配置文件. /etc/rc.d:启动的配 置文件和脚本. /home:用户主目录 ...