题目:

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来解决,先将str分割放到一个vector<string>中,再将pattern分割一起放入map<char,string>中,遍历判断是否是对应的键或值即可。

代码:

class Solution {
public:
bool wordPattern(string pattern, string str) {
stringstream ss(str);
string s;
vector<string> strVec;
while(ss >> s)
{
strVec.push_back(s);
} if(strVec.size() != pattern.size())
return false;
map<char, string> hash;
int i = ;
for(auto c : pattern)
{
if(hash.count(c))
{
if(hash[c] != strVec[i])
return false;
}
else
{
for(auto p : hash)
{
if(p.second == strVec[i])
return false;
}
hash[c] = strVec[i];
}
++i;
}
return true;
}
};

[LeetCode290]Word Pattern的更多相关文章

  1. [LeetCode] Word Pattern II 词语模式之二

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

  2. [LeetCode] Word Pattern 词语模式

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

  3. [LeetCode] Word Pattern

    Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...

  4. Word Pattern | & II

    Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...

  5. 291. Word Pattern II

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

  6. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  7. Word Pattern

    ​package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** *  * @au ...

  8. Word Pattern II 解答

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

  9. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

随机推荐

  1. 为什么php时间阅读RTF,p标签会出现红色

    为什么php读取富文本的时候,p标签会出现红线,怎么去掉,哪位大侠帮解决?跪求答案 就像以下一样,一遇到p标签就有红虚线 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  2. 一二三(The Seventh Hunan Collegiate Programming Contest)

    一二三 你弟弟刚刚学会写英语的一(one).二(two)和三(three).他在纸上写了好些一二三,可惜有些字母写错了.已知每个单词最多有一个字母写错了(单词长度肯定不会错),你能认出他写的啥吗? 输 ...

  3. liunx清理磁盘du -h --max-depth=1 /data/*

    liunx清理磁盘du -h --max-depth=1 /data/*

  4. TypeError: Cannot read property &#39;style&#39; of null 错误解决

    错误信息例如以下: JSP代码例如以下: <c:if test ="${not empty excelErrors}"> <div id="excelE ...

  5. 定义Java类的数组的问题

    定义了一个类: class Student{ private int Id; public int getId() { return Id; } public void setId(int id) { ...

  6. EBS并发管理器请求汇总(按照并发消耗时间,等待时间,平均等待事件等汇总)

    此数据集用于确定正在使用中并发管理器,并可与实际的在启动时分配的并发管理器.而且考虑完成状态为 正常/警告 的请求. select q.concurrent_queue_name, count(*) ...

  7. bootstrap在 刷新页面,tab选择页面不会改变。

    您可以直接复制代码 注意在同级别文件夹中引用 相应js 和 css. 实现tab影响 关键看bootstrap的 data-toggle= tab <html lang="en&quo ...

  8. 深入了解mysql它BDB系列(1)---BDB基础知识

        深入了解mysql它BDB系列(1) ---BDB关基础知识 作者:杨万富   一:BDB体系结构 1.1.BDB体系结构 BDB总体的体系结构如图1.1所看到的.包括五个子系统(见图1.1中 ...

  9. UVa 11408 - Count DePrimes

    题目:一个数的素因子的和假设也是素数就叫做DePrimes,统计给定区间内的DePrimes. 分析:数论.本题使用用一种素数的筛法,欧拉筛法,也加线性筛法. 这样的方法,每次删选分两种情况:1.素因 ...

  10. HDU4144:Bacon's Cipher

    Problem Description Bacon's cipher or the Baconian cipher is a method of steganography (a method of ...