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. Node.js学习笔记(6)——使用Express创建一个工程

    前提是搭建好了环境,node,npm,express:(推荐全局安装) 开始用express创建一个基础工程: express –t ejs microblog 进入文件夹之后 npm-install ...

  2. HTML/CSS开发规范指南

    参见文档:https://github.com/doyoe/html-css-guide 1.文档目录结构 |-- 项目名 |-- src 开发环境 |-- html 静态页面模板目录 |-- bgi ...

  3. web 表单方式上传文件方法(不用flash插件)

    原理:使用表单的input type="file"标签,通过ajax提交表单请求,后台获取请求中的文件信息,进行文件保存操作 由于我测试用的做了一个上传文件和上传图片方法,所以我有 ...

  4. [ACM] POJ 1068 Parencodings(模拟)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 De ...

  5. jdbc 模板 连接

    package itcast; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;i ...

  6. 小东和三个朋友一起在楼上抛小球,他们站在楼房的不同层,假设小东站的楼层距离地面N米,球从他手里自由落下,每次落地后反跳回上次下落高度的一半,并以此类推知道全部落到地面不跳,求4个小球一共经过了多少米?(数字都为整数) 给定四个整数A,B,C,D,请返回所求结果。

    include #include<vector> using namespace std; class Balls { public: int calcDistance(int A, in ...

  7. Mongodb亿级数据量的性能测试

    进行了一下Mongodb亿级数据量的性能测试,分别测试如下几个项目:   (所有插入都是单线程进行,所有读取都是多线程进行) 1) 普通插入性能 (插入的数据每条大约在1KB左右) 2) 批量插入性能 ...

  8. 如何创建虚拟硬盘 + os 读取硬盘参数代码

    [0]README 0.1) 本文旨在演示如何利用 bximage 创建虚拟硬盘: 0.2) 利用 os 读取硬盘参数, source code from orange's implemention ...

  9. 1213 - Deadlock found when trying to get lock; try restarting transaction

    1213 - Deadlock found when trying to get lock; try restarting transaction 出现这个原因要记住一点就是:innodb的行锁 和解 ...

  10. iOS程序自动检测更新的实现

      本文转载至 http://blog.csdn.net/davidsph/article/details/8931718 App Store自动更新itunes     之前项目需要用到app自动更 ...