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:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. 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(单词模式)的更多相关文章

  1. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. [LeetCode] 290. Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  3. 290 Word Pattern 单词模式

    给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...

  4. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  5. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  6. [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 ...

  7. 290. Word Pattern 单词匹配模式

    [抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  8. [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 ...

  9. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  10. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

随机推荐

  1. java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '=' 异常处理,及MySQL数据库编码设置

    java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,C ...

  2. 20145316《Java程序设计》第9周学习总结

    20145316<Java程序设计>第9周学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC(Ja ...

  3. python3_UUID模块详解

    1.知识背景 UUID是128位的全局唯一标识符,通常有32字节的字母表示.它可以保证时间和空间的唯一性. UUID——Universally unique identifier 在python中叫U ...

  4. Linux Java环境搭建

    -------------------------网络配置-------------------------------1.配置DNS:vi /etc/resolv.confnameserver 19 ...

  5. 在VMware中使用Nat方式设置静态IP

    为了在公司和家中不改变ip,所以采用vm的NAT模式来设置静态ip 1.vm采用NAT模式联网 2.编辑vm虚拟机设置 3.查看该网段的网关 可以看出网关为192.168.44.2,然后开始设置静态i ...

  6. 20145331 《Java程序设计》第4周学习总结

    20145331 <Java程序设计>第4周学习总结 教材学习内容总结 •第六章 1.继承的定义与特点: 子类继承父类的特征和行为,使得子类具有父类的各种属性和方法.或子类从父类继承方法, ...

  7. 0ctf2017-pages-choices

    Pages 题目来自于CCS 2016 <Prefetch Side-Channel Attacks: Bypassing SMAP and Kernel ASLR>,利用intel pr ...

  8. bzoj 1691: [Usaco2007 Dec]挑剔的美食家

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 621  Solved: 280[Submit][Status][Discuss] Description ...

  9. 何为K-邻近算法

    答:K-邻近算法,英文为K-nearest neighbor(KNN),就是计算要测试对象与k个样本对象之间的距离,通过距离的大小来对测试对象进行分类

  10. Quartz(自动任务)中的触发器Trigger

    1.Quartz中的触发器TriggerJob 包含了要执行任务的逻辑,但是 Job 对何时该执行却一无所知.这个事情留给了 Trigger.Quartz Trigger 继承了抽象的 org.qua ...