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记录就可以。

class Solution {
public:
bool wordPattern(string pattern, string str) {
int len_p=pattern.length();
int len_s=str.length();
vector<string>v();
int id=;
for(int i=;i<len_s;i++){
if(str[i]==' '){
id++;
v.push_back(string());
}
else{
v[id]+=str[i];
}
} if(len_p!=(id+)){
return false;
} else{
map<char,string>mp;
map<string,char>mp2; for(int i=;i<len_p;i++){
if(mp.count(pattern[i])==){
if(mp2.count(v[i])==){
mp[pattern[i]]=v[i];
mp2[v[i]]=pattern[i];
}
else{
return false;
}
}
else{
if(mp[pattern[i]]!=v[i]){
return false;
}
}
}
return true;
}
}
};

leetcode 290 Word Pattern(map的应用)的更多相关文章

  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 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

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

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

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

  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. Leetcode 290 Word Pattern STL

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

  7. Java [Leetcode 290]Word Pattern

    题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  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. 在 Selenium 中让 PhantomJS 执行它的 API

    from selenium import webdriver driver = webdriver.PhantomJS() script = "var page = this; page.o ...

  2. robot framework selenium2library定位

    进行页面元素操作,最麻烦的莫过于元素定位了,经常提示element is not visible 或者element is not exist 下面介绍常见的定位方法和定位中的问题 1 使用name和 ...

  3. Msfvenom 学习笔记与总结

    平台:Android,可用Payload: android/meterpreter/reverse_http Run a meterpreter server on Android. Tunnel c ...

  4. Chrome + Python 抓取动态网页内容

    用Python实现常规的静态网页抓取时,往往是用urllib2来获取整个HTML页面,然后从HTML文件中逐字查找对应的关键字.如下所示: import urllib2 url="http: ...

  5. 基于RedHat发行的Apache Tomcat本地提权漏洞

    描述 Tomcat最近总想搞一些大新闻,一个月都没到,Tomcat又爆出漏洞.2016年10月11日,网上爆出Tomcat本地提权漏洞,漏洞编号为CVE-2016-5425.此次受到影响的主要是基于R ...

  6. Android API Guides---Supporting Tablets and Handsets

    在Android平台上的各种屏幕尺寸的执行和系统调整大小正常应用程序的用户界面.以适应每一个人. 通常情况下,你须要做的是设计你的UI是灵活的,并通过提供替代资源(如又一次定位的一些看法观点或替代尺寸 ...

  7. 【WPF学习笔记】之如何点登录按钮时判断用户名密码进行登录:动画系列之(二)

    ...... 承接动画系列之(一)的代码: 再添加登录按钮代码进行登录,验证用户名和密码在数据库是否正确. 直接上代码: using System; using System.Collections. ...

  8. MySQL 原理性

    1.MySQL的复制原理以及流程 (1).复制基本原理流程 1. 主:binlog线程——记录下所有改变了数据库数据的语句,放进master上的binlog中: 2. 从:io线程——在使用start ...

  9. LeetCode222——Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  10. 认识XmlReader

    认识XmlReader   摘要 XmlReader类是组成.NET的关键技术之一,极大地方便了开发人员对Xml的操作.通过本文您将对XmlReader有一个很好的认识,并将其应用到实际开发中. 目录 ...