Problem:

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.

Summary:

判断所给字符串str中单词的形式与pattern中是否相符。

Solution:

以key为pattern中的char,value为str中的单词构成Hash Table,每加进一个新单词,判断原来若存在相同key的单词是否与新单词相同。

最终需排除不同key,value相同的情况。

 class Solution {
public:
bool wordPattern(string pattern, string str) {
vector<string> s;
unordered_map<char, string> m; int flag = ;
for (int i = ; i < str.size(); i++) {
if (str[i] == ' ') {
s.push_back(str.substr(flag, i - flag));
flag = i + ;
}
}
s.push_back(str.substr(flag, str.size() - flag)); if (pattern.size() != s.size()) {
return false;
} for (int i = ; i < pattern.size(); i++) {
if (m[pattern[i]] == "") {
m[pattern[i]] = s[i];
}
else if (m[pattern[i]] != s[i]){
return false;
}
} for (unordered_map<char, string>::iterator i = m.begin(); i != m.end(); i++) {
for (unordered_map<char, string>::iterator j = m.begin(); j != m.end(); j++) {
if (i->second == j->second && i->first != j->first) {
return false;
}
}
} return true;
}
};

LeetCode 290 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 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

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

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

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

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

  5. LeetCode 290. Word Pattern (词语模式)

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

  6. Leetcode 290 Word Pattern STL

    Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...

  7. Java [Leetcode 290]Word Pattern

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

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

  9. [leetcode] 290. Word Pattern (easy)

    原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...

随机推荐

  1. javascript判断数字是integer还是float

    function isFloat(n) { return n === +n && n !== (n|0); } function isInteger(n) { // 仅能检查32位的数 ...

  2. django models进行数据库增删查改

    在cmd 上运行 python manage.py shell   引入models的定义 from app.models import  myclass   ##先打这一行    ------这些是 ...

  3. linux文件目录结构

    1./ 文件系统的入口,最高一级目录 2./bin 存放基础系统所需的命令 3./boot 包含Linux内核及系统引导程序所需要的文件 4./dev 设备文件存储目录(声卡.磁盘.分区..) 5./ ...

  4. git: 修改commiter 信息

    Committer: root root@localhost.localdomain 您的姓名和邮件地址基于登录名和主机名进行了自动设置.请检查它们正确 与否.您可以通过下面的命令对其进行明确地设置以 ...

  5. 了解PHP中的register_shutdown_funcion

    (PHP 4, PHP 5, PHP 7) register_shutdown_function - Register a function for execution on shutdown 执行P ...

  6. BootLoader(2440)核心初始化代码

    1.gboot.lds OUTPUT_ARCH(arm)ENTRY(_start)SECTIONS {    . = 0x30008000; //起始地址        . = ALIGN(4);// ...

  7. eclipse maven项目错误

    eclipse maven项目错误:Failure to transfer org.codehaus.plexus:plexus-interpolation:jar:1.15 from http:// ...

  8. jQuery文本框中的事件应用

    jQuery文本框中的事件应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...

  9. js function集合

    /*跳转并是刷新界面*/ function page_back(){ history.go(-); location.reload(); } function show_div(obj){ if(ob ...

  10. thinkphp 3.2 join

    $res2 = M('stat_info a') ->join(C('DB_PREFIX').'stock b ON a.goods_id = b.goods_id') ->field(' ...