290 Word Pattern 单词模式
给定一种 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 单词模式的更多相关文章
- [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 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- [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 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- CentOS7下搭建postfix邮箱服务器并实现extmail的web访问
http://blog.51cto.com/zero01/2064693 https://blog.csdn.net/a5nan/article/details/52510887
- mysql配置文件my.ini的修改问题
修改innodb_buffer_pool_size这个参数一般都没问题,但是修改innodb_log_file_size这个参数如果过大,mysql日志就会提示: Error: log file .\ ...
- Ubuntu 16.04下减小/释放/清理VirtualBox虚拟硬盘文件的大小
一般在VirtualBox中安装Windows,然后用无缝模式进行某些特定软件的使用. 而VirtualBox的虚拟硬盘会越用越大,并且VirtualBox没有自带清理工具,相比VMware来说,VM ...
- Java导出jar并运行
1. 创建manifest.mf文件 在工程下创建manifest.mf文件.文件内容为: Manifest-version: 1.0 Main-Class: SqlCheckUtil.java 注意 ...
- 使用Hexo搭建博客
好长时间没在博客园写东西了,自己搭了一套博客,把自己的一些积累分享给大家,欢迎指正! 博客地址:http://www.baiguangnan.com 使用Hexo搭建自己的博客,可以参考这里:http ...
- FaceBook开源库Fresco
讨论学习使用 关于 Fresco Fresco 是一个强大的图片载入组件. Fresco 中设计有一个叫做 image pipeline 的模块.它负责从网络.从本地文件系统.本地资源载入图片. 为了 ...
- Java 实现一个链表
public class MyList { static class Node {// 节点类 Object data; Node next; public Node(Object data) {// ...
- hdu 4864 Task(贪心)
pid=4864">http://acm.hdu.edu.cn/showproblem.php?pid=4864 大致题意:有n台机器和m个任务,都有两个參数工作时间time和难度le ...
- 模式识别之车牌识别---一个开源车牌识别项目easypr
http://doc.okbase.net/subconscious/archive/105312.html https://github.com/liuruoze http://www.cnblog ...
- JavaScript实现页面重载 - 535种方式
location = location ... and a 534 other ways to reload the page with JavaScript location = location ...