Word Pattern【熟练map容器】

3人阅读 评论(0) 收藏 举报
分类:

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.

SOLVE:

bool wordPattern(string pattern, string str) {
    map<char, int> p2i;
    map<string, int> w2i;
    istringstream in(str);
    int i = 0, n = pattern.size();
    for (string word; in >> word; ++i) {
        if (i == n || p2i[pattern[i]] != w2i[word])
            return false;
        p2i[pattern[i]] = w2i[word] = i + 1;
    }
    return i == n;
}

注:这题主要让自己熟练 C++ STL 中的 map 容器运用。

查看评论

其实你还不懂Word

本课程根据多年的工作及企业培训经验,总结了大多数人使用Word的误区及存在的问题。以如何提高排版效率为最终目的,从排版基本原则及规范化入手,结合实例讲述排版的思想、理念、技巧、功能、方法,力求以最合理的结构、最简洁的语言为大家奉上最全面、最实用的内容。
  • 2018年04月03日 05:18

[leetcode] 291. Word Pattern II 解题报告

题目链接: https://leetcode.com/problems/word-pattern-ii/ Given a pattern and a string str, find i...
  • qq508618087
  • qq508618087
  • 2016-06-24 04:20:47
  • 1907

[LeetCode 290] Word Pattern

Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "a...
  • sbitswc
  • sbitswc
  • 2015-10-09 11:44:41
  • 3691

leetcode 290: Word Pattern

Word Pattern Total Accepted: 1733 Total Submissions: 6204 Difficulty: Easy Given a...
  • xudli
  • xudli
  • 2015-10-06 16:10:22
  • 5150

Word Pattern问题及解法

问题描述: Given a pattern and a string str, find if str follows the same pattern. Here follow mean...
  • u011809767
  • u011809767
  • 2017-04-04 11:27:26
  • 209

290. Word Pattern [easy] (Python)

题目链接https://leetcode.com/problems/word-pattern/题目原文 Given a pattern and a string str, find if str ...
  • coder_orz
  • coder_orz
  • 2016-06-16 17:48:27
  • 2545

LeetCode 291. Word Pattern II

BackTracking. #include #include #include #include using namespace std; /* Given a pattern an...
  • github_34333284
  • github_34333284
  • 2016-05-27 12:20:46
  • 283

LeetCode——Word Pattern的JavaScript的实现

Given a pattern and a string str, find if str follows the same pattern. Examples: pattern ...
  • jsdchenye
  • jsdchenye
  • 2015-10-09 15:26:42
  • 474

leetcode 290 Word Pattern C++

这题我没有用istringstream,因为如果不是空格就不能用了,所以要用一个一般性的办法。 bool wordPattern(string pattern, string str) { ...
  • a2331046
  • a2331046
  • 2016-06-07 21:44:23
  • 470

leetcode_c++:哈希:word pattern(290)

Given a pattern and a string str, find if str follows the same pattern.Here follow means a full matc...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-07-10 14:59:02
  • 279
    个人资料
    持之以恒
    等级:
    访问量: 1027
    积分: 248
    排名: 25万+
    文章存档