LeetCode OJ:Word Pattern(单词模式)
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:
- pattern =
"abba"
, str ="dog cat cat dog"
should return true. - pattern =
"abba"
, str ="dog cat cat fish"
should return false. - pattern =
"aaaa"
, str ="dog cat cat dog"
should return false. - 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) {
stringstream ss(str);
vector<string> tmpVec;
string tmp;
while(ss >> tmp)
tmpVec.push_back(tmp);
if(tmpVec.size() != pattern.size())
return false;
map<char, string> m1;
map<string, char> m2;
for(int i = ; i < pattern.size(); ++i){
tmp = tmpVec[i];
if(m1.find(pattern[i]) == m1.end() && m2.find(tmp) == m2.end()){
m1[pattern[i]] = tmp;
m2[tmp] = pattern[i];
}else if(m1.find(pattern[i]) != m1.end() && m2.find(tmp) != m2.end()){
if(m1[pattern[i]] != tmp || m2[tmp] != pattern[i])
return false;
}else{
return false;
}
}
return true;
}
};
LeetCode OJ: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 ...
- [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 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...
- 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] 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 ...
- 290. Word Pattern 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
随机推荐
- 今天刚接触lua 写写环境配置和基本设置
首先是安装开发工具: Quick: http://www.tairan.com/engines-download 引擎 Sublime Text的下载地址为:http://www.sublimetex ...
- Ubuntu16.04 Xmind安装
Ubuntu16.04 Xmind安装: 官网下载:直接下载不用注册: 到下载目录选择下载文件右键,提取到此处(为登录用户权限):在终端采用root权限解压为root权限(unzip xmind-8- ...
- Flash Builder4注册机
我的Eclipse下的Flash Builder 4正式版已经过期,之前在网上找到的注册码,都不能用了, 花了很久时间,才找到这个注册机. Flash Builder 4 注册机 Serial Cra ...
- igraph Tutorial
igraph Tutorial¶ 参考http://www.cs.rhul.ac.uk/home/tamas/development/igraph/tutorial/tutorial.html ...
- javascript 面向对象 new 关键字 原型链 构造函数
JavaScript面向对象JavaScript 语言使用构造函数(constructor)作为对象的模板.所谓"构造函数",就是专门用来生成实例对象的函数.它就是对象的模板,描述 ...
- hadoop hdfs设置quota
quota分为两种: 1. 目录下的文件数限制 2. 目录下的空间大小 //设置文件数 hdfs dfsadmin -setQuota 1000000 /user/jenkin //设置空间大小 hd ...
- Junit中的setup和teardown方法
setup需要@before注解,实现测试前的初始化工作 teardown需要@after注解,测试完成后垃圾回收等后续工作
- lamp编译详解
首先确认系统环境:centos6.4 min版本 1.安装需要的开发环境 yum groupinstall "Development Tools" "Server Pla ...
- mongodb入门很简单(3)
##简单的mongodb入门命令## 1.show dbs; //查看当前数据库 2.use databaseName; //选库 3.show tables/collec ...
- [BZOJ4003]城池攻占
Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, ...