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 ...
随机推荐
- iconnect
https://iconnect.infosysapps.com/vpn/index.html
- $python日期和时间的处理
总结一下python中对日期和时间的常用处理方法. 准备 import time,datetime 常用操作 输出当前的日期时间 方式一: now = time.localtime() print ' ...
- iOS知识点整理
1.宏定义 #define # ## a. 后面一个#是转成字符串 b. 后面## 是连接的作用 2. __attribute__ 常用的 __attribute__(( constructor ...
- netty6---序列化与反序列化
package com.cn; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import ja ...
- servlet的总结
tomcat在启动的时候 加载webapp下面的web.xml,加载里面定义的servlet. web.xml文件有两部分:servlet类定义和servlet映射定义每个被载入的servlet类都有 ...
- cogs 341:[NOI2005] 聪聪与可可
★★ 输入文件:cchkk.in 输出文件:cchkk.out 简单对比 时间限制:1 s 内存限制:256 MB [问题描述] 在一个魔法森林里,住着一只聪明的小猫聪聪和一只可爱的小 ...
- TensorFlow和深度学习入门教程(TensorFlow and deep learning without a PhD)【转】
本文转载自:https://blog.csdn.net/xummgg/article/details/69214366 前言 上月导师在组会上交我们用tensorflow写深度学习和卷积神经网络,并把 ...
- DataStage系列教程 (Slowly Changing Dimension)缓慢变化维
BI中维表的增量更新一般有2种: Type 1:覆盖更改.记录的列值发生变化,直接update成最新记录. Type 2:历史跟踪更改.记录值发生变化,将该记录置为失效,再insert一条新的记录. ...
- 【pytorch】pytorch基础学习
目录 1. 前言 # 2. Deep Learning with PyTorch: A 60 Minute Blitz 2.1 base operations 2.2 train a classifi ...
- STL set集合用法总结(multiset)
2017-08-20 15:21:31 writer:pprp set集合容器使用红黑树的平衡二叉树检索树,不会将重复键值插入,检索效率高 logn 检索使用中序遍历,所以可以将元素从小到大排列出来 ...