Word Pattern

Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy

Given a pattern and a string str, find if str follows the same pattern.

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:

  1. patterncontains only lowercase alphabetical letters, and str contains words separated by a single space. Each word in str contains only lowercase alphabetical letters.
  2. Both pattern and str do not have leading or trailing spaces.
  3. Each letter in pattern must map to a word with length that is at least 1.

Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.

/**
* @param {string} pattern
* @param {string} str
* @return {boolean}
*/
var wordPattern = function(pattern, str) {
var s_to_p = {};
var p_to_s = {};
var pi = 0;
var si = 0;
if (!pattern || !str) return false;
while (pi < pattern.length) {
if (si >= str.length) return false;
var word = "";
var pt = pattern[pi];
while (si < str.length && str[si] != " ") {
word += str[si];
si++;
}
si++;
if (!s_to_p[word]) {
s_to_p[word] = pattern[pi];
}
else {
if (s_to_p[word] != pattern[pi]) return false;
} if (!p_to_s[pt]) {
p_to_s[pt] = word;
}
else {
if (p_to_s[pt] != word) return false;
}
pi++;
}
if (si < str.length) return false;
return true;
};

最简单的方法利用两个哈希表做双向的对应,主要容易错的是在各种edge cases,比如pattern 和 str 长度不对应,为空等。

 
 

[LeetCode] 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 II

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

  4. LeetCode Word Pattern (模拟)

    题意: 给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern. 思路: 注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同, ...

  5. Leetcode solution 291: Word Pattern II

    Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...

  6. leetcode面试准备: Word Pattern

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

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

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

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

    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 单词模式

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

随机推荐

  1. U盘安装中标麒麟服务器操作系统 一 (NeoKylin 6.5)

    U盘安装中标麒麟服务器操作系统(NeoKylin 6.5) 首先需要下载中标麒麟服务器操作系统的iso镜像.我这里的是NeoKylin Linux A 6.5.iso 因为超过了4GB,百度网盘不支持 ...

  2. html之左边不动右边内容自动修剪并出现滚动轮查看剩余内容

    <html lang="en"> <head> <meta charset="UTF-8"> <title>Ti ...

  3. 11.3---旋转有序数组之后查找元素(CC150)

    思路,这道题用二分,唯一的不同就是,1,a[left]<a[mid].那么说明左右有序,如果key还在a[left],a[mid]之间,就在这里找,如果不在就在右边找.注意:这里<要改成& ...

  4. spring ext 跨域

    read方法中调用的response对象是父类BaseController的一个成员变量. spring默认bean的生命周期Score为singleton单例模式. 当多线程并发使用同一bean, ...

  5. 使用 PHP 7 给 Web 应用加速

    PHP 20周年了!?? PHP 首发通告,1995年6月8日 发布于 COMP.INFOSYSTEMS.WWW.AUTHORING.CGI 主题:正式宣布:个人主页工具(Personal Home ...

  6. vim常用命令(iOS)

    iOS下vim的使用: vim 的三种模式: .一般模式(默认) .插入模式(写文字) .命令行模式(保存) 各种模式的功能区分如下: .一般模式:控制屏幕光标的移动,字符和光标的删除,移动复制某区段 ...

  7. RRD

    http://my.oschina.net/u/1458120/blog/208857

  8. 饿了么 openapi demo

    http://merchant.openapi.eleme.io/merchant.html#id215 class Program { static void Main(string[] args) ...

  9. FastReport报表控件使用技巧总结

    FastReport报表控件使用技巧总结 1.FastReport中如何访问报表中的对象? 可以使用FindObject方法. TfrxMemoView(frxReport1.FindObject(' ...

  10. Python 之 【re模块的正则表达式学习】

    摘要: re模块包括操作正则表达式的函数,一些工作中都需要用到,现在说明下使用方法. 使用说明: 一,re模块下的函数:            函数             描述 compile(pa ...