leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern
istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割。
C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件。
istringstream类用于执行C++风格的串流的输入操作。
ostringstream类用于执行C风格的串流的输出操作。
strstream类同时可以支持C风格的串流的输入输出操作。
1.本题中str是带空格的字符串,需要将每个词根据提取出来,就使用istringstream。
2.本题要求的是pattern中的字符和str中的单词是一一对应的,所以不仅要判断当前pattern字符与存储在hash中的的映射是否相同,还需要判断hash中映射的string是否与当前string相同。
3.可能存在pattern和str个数不一样的情况,所以最后使用i == n来判断。
class Solution {
public:
bool wordPattern(string pattern, string str) {
unordered_map<char,string> m;
istringstream in(str);
int i = ,n = pattern.size();
for(string tmp;in >> tmp;i++){
if(m.find(pattern[i]) == m.end()){
for(auto it = m.begin();it != m.end();it++){
if(it->second == tmp)
return false;
}
m[pattern[i]] = tmp;
}
else{
if(m[pattern[i]] != tmp)
return false;
}
}
return i == n;
}
};
这种情况就是两者个数不相同。
Input:
"jquery"
"jquery"
Output:
true
Expected:
false
829. Word Pattern II
https://www.cnblogs.com/grandyang/p/5325761.html
不知道哪个单词对应哪个词,就用递归去搜索每种可能性
class Solution {
public:
/**
* @param pattern: a string,denote pattern string
* @param str: a string, denote matching string
* @return: a boolean
*/
bool wordPatternMatch(string &pattern, string &str) {
// write your code here
unordered_map<char,string> m;
int index1 = ,index2 = ;
return wordPatternMatch(pattern,index1,str,index2,m);
}
bool wordPatternMatch(string &pattern,int index1,string &str,int index2,unordered_map<char,string> &m){
if(index1 == pattern.size() && index2 == str.size())
return true;
if(index1 == pattern.size() || index2 == str.size())
return false;
char word = pattern[index1];
for(int i = index2;i < str.size();i++){
string tmp = str.substr(index2,i - index2 + );
if(m.count(word) && m[word] == tmp){
if(wordPatternMatch(pattern,index1+,str,i+,m))
return true;
}
else if(!m.count(word)){
bool flag = false;
for(auto it : m){
if(it.second == tmp)
flag = true;
}
if(!flag){
m[word] = tmp;
if(wordPatternMatch(pattern,index1+,str,i+,m))
return true;
m.erase(word);
}
}
}
}
};
自己又写了一个版本:
其实主要是如果当前不满足true的条件,就继续遍历
if(m.find(word) != m.end()){
if(m[word] == tmp){
if(wordPatternMatch(pattern,str,index1+1,i+1,m))
return true;
}
}
如果word != tmp,不做任何处理直接继续遍历下一个位置就好了
if(flag)
continue;
如果在map中找到了tmp,也就不满足条件,这个时候也不做任何处理,直接继续遍历就好了
class Solution {
public:
/**
* @param pattern: a string,denote pattern string
* @param str: a string, denote matching string
* @return: a boolean
*/
bool wordPatternMatch(string &pattern, string &str) {
// write your code here
unordered_map<char,string> m;
int index1 = ,index2 = ;
return wordPatternMatch(pattern,str,index1,index2,m);
}
bool wordPatternMatch(string pattern,string str,int index1,int index2,unordered_map<char,string> m){
if(index1 == pattern.size() && index2 == str.size())
return true;
else if(index1 == pattern.size() || index2 == str.size())
return false;
char word = pattern[index1];
for(int i = index2;i < str.size();i++){
string tmp = str.substr(index2,i - index2 + );
if(m.find(word) != m.end()){
if(m[word] == tmp){
if(wordPatternMatch(pattern,str,index1+,i+,m))
return true;
}
}
else{
bool flag = false;
for(auto it = m.begin();it != m.end();it++){
if(it->second == tmp){
flag = true;
break;
}
}
if(flag)
continue;
m[word] = tmp;
if(wordPatternMatch(pattern,str,index1+,i+,m))
return true;
m.erase(word);
}
}
}
};
leetcode 290. Word Pattern 、lintcode 829. Word Pattern II的更多相关文章
- lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II
变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- [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 ...
- [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
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- Java [Leetcode 290]Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- leetcode 290 Word Pattern(map的应用)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
随机推荐
- MySQL 主从延迟几万秒 Queueing master event to the relay log(转)
数据库版本Server version: 5.6.24-log Source distribution 问题描述 数据采集平台业务数据库由于批量灌数据导致主从延迟上万秒. 复制线程长期处于Que ...
- win10 64下anaconda4.2.0(python3.5)
python环境:win10 64下anaconda4.2.0(python3.5).安装tensorflow过程是在Anaconda Prompt中进行安装 1:打开Anaconda Prompt ...
- 51nod 2517 最少01翻转次数
小b有一个01序列,她每次可以翻转一个元素,即将该元素异或上1. 现在她希望序列不降,求最少翻转次数. 收起 输入 第一行输入一个数n,其中1≤n≤20000: 第二行输入一个由‘0’和‘1’组成 ...
- TODO : 一些新的学习计划
1.读完jvm那本书 2.加深Android的开发知识 3.编写atx的demo 4.跑几个apk的性能测试并做详细的性能分析 5.尝试实现一个uiautomator多个手机同时执行脚本的可能性(连线 ...
- redis 缓存问题,转载:https://www.cnblogs.com/liangsonghua/p/www_liangsonghua_me_22.html
缓存穿透: 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且处于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义 ...
- set_index
Signature: df.set_index( ['keys', 'drop=True', 'append=False', 'inplace=False', 'verify_integrity=Fa ...
- Java邮件开发(三):解决附件名为乱码及显示友好名称
1.附件的名称只能为英文,中文乱码 2.友好名称的显示. 我们使用163等邮箱发送邮件时,我们经常可以看到收件人一栏中会是:张益达 <zyh5540@163.com>这种方式.在上一版本的 ...
- iota妙用
itoa可以套公式,下面的依旧会按照公式运算 package main import "fmt" func main() { const ( b = 1 << (10 ...
- 关于JS变量和作用域详解
ECMAScript 变量: 1.基本类型值(简单数据段) 2.引用类型值(可能由过个值构成的对象) → 保存在内存中的对象 ------ 动态属性: 只能给引用型值动态添加新属性,以便将来使用. - ...
- P3293 [SCOI2016]美味 主席树+按位贪心
给定长度为 \(n\) 序列 \(a[i]\) ,每次询问区间 \([l,r]\) ,并给定 \(b,x\) 中的一个数 \(p=a[i]\) ,使得最大化 \(b \bigoplus p^x\) 主 ...