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.

Examples:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. pattern = "abba", str = "dog dog dog dog" should return false.

Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

思路:使用两个map记录是否pattern和str匹配。

注意,map的键值这里不能为0,因为为0时map默认是访问了未创建过的键,因此要从1开始。

此外,for循环里真的可以实现很多东西,用好了会让代码很精简。

 class Solution {
public:
bool wordPattern(string pattern, string str) {
map<char, int> dict1;
map<string, int> dict2;
stringstream ss(str);
int i = , n = pattern.size();
for (string word; ss >> word; i++)
{
if (i == n || dict1[pattern[i]] != dict2[word])
return false;
dict1[pattern[i]] = dict2[word] = i + ;
}
return i == n;
}
};

Word Pattern - LeetCode的更多相关文章

  1. [LeetCode] Word Pattern II 词语模式之二

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

  2. [LeetCode] Word Pattern 词语模式

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

  3. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

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

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

  5. Leetcode solution 291: Word Pattern II

    Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...

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

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

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

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

  8. [LeetCode] 291. Word Pattern II 词语模式 II

    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) 41

    290. 单词规律 290. Word Pattern 题目描述 给定一种规律 pattern 和一个字符串 str,判断 str 是否遵循相同的规律. 这里的 遵循 指完全匹配,例如,pattern ...

随机推荐

  1. 2019腾讯暑期实习面试(offer)前端

    最近在忙着准备找实习,所以没有更新之前的文章. 不过所幸功夫不负有心人,我拿到了腾讯的offer. 这里分享一下面试的经验. 简介 本人双非本科,普通学生一枚. 面的是腾讯的Web前端开发. 整个面试 ...

  2. 5、CSS基础part-3

    1.CSS列表 ①类型 ul.disc {list-style-type: disc} ②位置 ul.inside {list-style-position: inside} ③列表图像 2.表格

  3. 【Generate Parentheses】cpp

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  4. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  5. MFC深入浅出读书笔记第三部分1

    第八章 Document-View 深入探讨(总结) 1.Document Document 在MFC 的CDocument 里头被具体化.CDocument 本身并无任何具体数据,它只是提供一个空壳 ...

  6. [oldboy-django][2深入django]rest-framework教程

    # rest-framework教程 - settings.py INSTALL-APPS = [ 'snippets', # app 'rest-framework', ] - 创建model # ...

  7. java io 流 输入输出 大牛经典总结

    在软件开发中,数据流和数据库操作占据了一个很重要的位置,所以,熟悉操作数据流和数据库,对于每一个开发者来说都是很重要的,今天就来总结一下I/O,数据库操作 一:从数据流开始 首先先有一个结构图看一下整 ...

  8. 2015暑假训练(UVALive 5983 - 5992)线段树离线处理+dp

    A: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83690#problem/A 题意:N*M的格子,从左上走到右下,要求在每个点的权值 ...

  9. 在iBatis中操作Blob数据类型

    这里的Blob数据类型指的是保存了文本的blob数据类型 直接读取blob类型存储的文本,可能会出现乱码,所以需要读取完后进行手动转码 这里使用ibatis作为持久层 SELECT urlconten ...

  10. 【bzoj3295】[Cqoi2011]动态逆序对 线段树套SBT

    题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...