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 ...
随机推荐
- pipeline和channel的区别
pipeline和channel的区别 在golang中,学到channel时,往往都会产生一些疑惑,和channel的区别是什么? 以下就是区别: difference channel pipeli ...
- 桂电深信服CTF之MSC真假压缩包
真假压缩包-复现(吐槽一句,脑洞真特么大) 先从虚假的入手,一开始猜测虚假的是伪加密,后面也确实是 后面打开压缩包,是一个txt文件 一眼可以看出来是RSA,算出来明文是5(于是就在这里卡了两天,一直 ...
- https跳http
listen 443 ssl;rewrite ^ http://$http_host$request_uri? permanent;
- Kotlin星投影与泛型约束详解
星投影(star projection): 继续来学习Kotlin泛型相关的东东,星投影(star projection),这是个啥东东呢?下面先来说一下概念: 1.对于Star<out T&g ...
- 布隆过滤器(Bloom Filter)-学习笔记-Java版代码(挖坑ing)
布隆过滤器解决"面试题: 如何建立一个十亿级别的哈希表,限制内存空间" "如何快速查询一个10亿大小的集合中的元素是否存在" 如题 布隆过滤器确实很神奇, 简单 ...
- 《3+1团队》【Beta】Scrum meeting 1
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...
- vue引入js文件时报This dependency was not found:错误
vue引入js文件时报This dependency was not found:错误 使用了很多方法,原来是这么小的问题,特此记录 解决办法 添加 ./
- 域渗透:IPC$ 命名管道
介绍:IPC$(Internet Process Connection) 是共享 " 命名管道 " 的资源,它是为了让进程间通信而开放的命名管道,通过提供可信任的用户名和口令,连接 ...
- SIGAI机器学习第二十一集 AdaBoost算法2
讲授Boosting算法的原理,AdaBoost算法的基本概念,训练算法,与随机森林的比较,训练误差分析,广义加法模型,指数损失函数,训练算法的推导,弱分类器的选择,样本权重削减,实际应用. 大纲: ...
- BlockingCollection<T> 类实现 列队操作
官方文档 为实现 IProducerConsumerCollection<T> 的线程安全集合提供阻塞和限制功能. 通过 BlockingCollection<T> 实现列队调 ...