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 ...
随机推荐
- jmeter+http接口测试
参考: http://blog.csdn.net/github_27109687/article/details/71968662 Jmeter接口测试+压力测试 http://blog.csdn ...
- Android Camera API ISO Setting
https://stackoverflow.com/questions/2978095/android-camera-api-iso-setting exif this.mCameraParamete ...
- Web安全学习笔记之Nmap脚本使用指南
nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端.确定哪些服务运行在哪些连接端,并且推断计算机运行哪个操作系统.它是网络管理员必用的软件之一,以及用以评估网络系统安全. —— 来自百 ...
- Kafka学习之(一)了解一下Kafka及关键概念和处理机制
Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模小打的网站中所有动作流数据.优势 高吞吐量:非常普通的硬件Kafka也可以支持每秒100W的消息,即使在非常廉价的商用机器上也能做 ...
- 如何用纯 CSS 创作气泡填色的按钮特效
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eKqZjy 可交互视频 ...
- MyBatis如何返回自增的ID
<insert id="insertTable" parameterType="com.infohold.city.map.model.CheckTemplateM ...
- z-albert之开启博文之路
其实注册博客园已经蛮久的了,一直都只是停留在看,却没有自己动手一篇属于自己的技术博文.之所以以前一直没写,以前没有工作,一直都是小白.然而今天为什么感写了呢,并不是自己比以前懂得多多少,而是希望将自己 ...
- uboot源码中"include/configs/$(boardname).h"与"configs/$(boardname)_defconfig"之间有何异同
答:最大的不同就是"configs/boardname_defconfig"中的选项都可以在make menuconfig中进行配置,而"include/configs/ ...
- git gc内存错误的解决方案
Auto packing the repository for optimum performance. You may alsorun "git gc" manually. Se ...
- 【视觉基础知识】Bag of words 在图像中的应用
文章转载自:https://www.cnblogs.com/shihuajie/p/5782515.html BOW (bag of words) 模型简介 Bag of words模型最初被用在文本 ...