给定一种 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. js格式化日期时间

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).周(E).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1 ...

  2. 搬砖--杭电校赛(dfs)

    搬砖 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submissi ...

  3. CAS--CompareAndSwap原理

    1.CAS(Compare-and-Swap),即比较并替换,是一种实现并发算法时常用到的技术,Java并发包中的很多类都使用了CAS技术. CAS需要有3个操作数:内存地址V,旧的预期值A,即将要更 ...

  4. MicroPython实现wifi干扰与抓包

    0×00前言 之前做的WIFI攻击实验都是基于arduino环境开发的,最近想尝试一下使用micropython完成deautch(解除认证)攻击.本次开发板使用的还是TPYBoardv202. 0× ...

  5. Linux system log avahi-daemon[3640]: Invalid query packet.

    2014-06-11 Check the Linux system log find the errorr: Jun  9 11:18:49 hostname avahi-daemon[3640]: ...

  6. hadoop(九) - hbase shell命令及Java接口

    一. shell命令 1. 进入hbase命令行  ./hbase shell 2. 显示hbase中的表  list 3. 创建user表,包括info.data两个列族 create 'user' ...

  7. CBO之Full Table Scan - FTS算法

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/44261859 ************************************** ...

  8. fused multiply and add

    1 要解决的问题 计算x*y + z?其中x.y.z都是浮点数. 2 普通的计算方式 e=3; s=4.734612 × e=5; s=5.417242 ----------------------- ...

  9. train_action

    # 导入数值计算模块 import numpy as np import tensorflow as tf # 创建计算会话 sess = tf.Session() # 生成数据,创建占位符和变量A ...

  10. Git经常使用命令总结

    Git是一款开源的分布式版本号控制系统,由Linux之父Torvalds用C语言开发. "the stupid content tracker",Git自诩为stupid,却是一个 ...