给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式。
这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系。
例如:
    pattern = "abba", str = "dog cat cat dog", 返回true
    pattern = "abba", str = "dog cat cat fish", 返回false.
    pattern = "aaaa", str = "dog cat cat dog" , 返回false.
    pattern = "abba", str = "dog dog dog dog" , 返回false.
说明:
你可以假设 pattern 只包含小写字母, str 包含了由单个空格分开的小写单词。

详见:https://leetcode.com/problems/word-pattern/description/

class Solution {
public:
bool wordPattern(string pattern, string str) {
unordered_map<char,int> m1;
unordered_map<string,int> m2;
istringstream in(str);
int i=0;
for(string word;in>>word;++i)
{
if(m1.find(pattern[i])!=m1.end()||m2.find(word)!=m2.end())
{
if(m1[pattern[i]]!=m2[word])
{
return false;
}
}
else
{
m1[pattern[i]]=m2[word]=i+1;
}
}
return i==pattern.size();
}
};

参考:https://www.cnblogs.com/grandyang/p/4857022.html

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. 290. Word Pattern 单词匹配模式

    [抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  3. [LeetCode] 290. Word Pattern 词语模式

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

  4. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

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

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

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

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

  7. [LeetCode] Word Pattern 词语模式

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

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

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

  9. 【一天一道LeetCode】#290. Word Pattern

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. scp远程文件传输

    第一次.提示下载公钥 [root@rhel5 ~]# scp install.log root@192.168.124.129:/tmp The authenticity of host '192.1 ...

  2. Ubuntu 16.04安装Ubuntu After Install工具实现常用软件批量安装

    这个软件集成了常用且好用的软件,且只需要选择需要的软件之后自动安装好,不需要额外设置. 安装: sudo add-apt-repository ppa:thefanclub/ubuntu-after- ...

  3. JSP中HTTP状态码

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/http-status-codes.html: HTTP请求格式和HTTP响应消息的格式一样,都有以下结构 ...

  4. Android GIS开发系列-- 入门季(12) 显示载天地图

    在项目中可以经常需要动态加载一些图层,像投影地图服务.投影地图服务器.其实网上有大量这样的服务,比如天地图官网, . 随便点开一个服务,里面有相关的信息.那如何加载这样图层服务呢. 一.首先感谢这篇博 ...

  5. 【转】Linux软连接和硬链接

    再次温习一下,操作的不多.虽然感觉都会!!!! 这次再次操作一遍!! 通过上面的测试发现,删除f1之后,软连接f3就无效了,硬链接f3则不受影响. ls -F可以看到文件的类型. 连接文件的作用? - ...

  6. JAVA程序—HelloWorld 的编译运行

    在我们写好我们的"HelloWorld.java",并且搭建好JAVA的开发环境过后,便可以运行这个JAVA程序了. 具体如何实现,让我们来看看: 打开DOS 通过DOS命令转到& ...

  7. yarn-cli 简介

    Yarn 提供了丰富的命令行命令来帮你应付你所开发的 Yarn 包的方方面面的工作,包括安装.管理.发布等等. 虽然我们在这里按照字母顺序列出了我们提供的所有命令,但是某些更常用的命令还是要着重提一下 ...

  8. web常见之音乐播放器

    代码来源于:这位Github小伙伴,我只负责解说! 效果图如下: 先上HTML代码 源码: <!DOCTYPE html> <html> <head> <me ...

  9. HDU4930-Fighting the Landlords

    题意:斗地主,就是要自己出牌.使得对手在这一轮无法出牌,或者有出牌的可能.可是你的牌已经走完了.假设符合这些条件的话,输出Yes.否则输出No. 思路:先预处理能直接把牌走完的情况,假设不行的话就直接 ...

  10. 我在CSDN开通博客啦!

    今天,我最终在CSDN开通博客啦!