Problem Statement

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.
Example 1:
Input: pattern = "abba", str = "dog cat cat dog"
Output: true
Example 2:
Input:pattern = "abba", str = "dog cat cat fish"
Output: false
Example 3:
Input: pattern = "aaaa", str = "dog cat cat dog"
Output: false
Example 4:
Input: pattern = "abba", str = "dog dog dog dog"
Output: false
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.

Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

A very simple problem thus normally can solve it in multiple ways.

Encode string and patterns then compare

Since we are comparing "patterns" here, one straightforward way is encode the pattern into a string, then use the same encoding algorithm to encode the str array into a string, then compare the string.

What encoding should we choose? Well it's not really an encoding per se. What I did is just convert any word or character to a character staring with ('a' + an index). If we see this character before, we just directly return from the hash map lookup. For example, "duck dog dog" would be encoded as "abb" while "bcc" would also be encoded as "abb".

Use bijection mapping

Note in the problem description it mentions it is a bijection mapping (i.e., a one to one mapping).
As shown in the graph below, you see the differences between injection, surjection and bijection. That said, bijection does not allow duplicates. We can build a one to one mapping between the pattern and string, since it's bijection, if two characters in the pattern map to the same string, then it's not a valid bijection, therefore return false.

Ref: https://en.wikipedia.org/wiki/Injective_function
 

Solutions

Encode string and patterns then compare

  // This way will also work, just a little bit more work by encoding each string into the same one
// Kinda similar to the isomorphic string
public boolean wordPatternEncoding(String pattern, String str) {
if (str == null || str.isEmpty() || pattern == null || pattern.isEmpty()) {
return false;
} String[] s = str.split(" ");
if (pattern.length() != s.length) {
return false;
} // encode pattern
String patternEncoded = this.encodeString(pattern);
// encode the string array
String strEncoded = this.encodeArray(s); // compare
return patternEncoded.equals(strEncoded);
} private String encodeArray(String[] s) {
Map<String, Character> lookup = new HashMap<>(); int index = 0; // starting from 'a'
StringBuilder sb = new StringBuilder();
for (String ss : s) {
if (lookup.containsKey(ss)) {
sb.append(lookup.get(ss));
} else {
char c = (char)('a' + index);
sb.append(c);
index++;
lookup.put(ss, c);
}
}
return sb.toString();
} // encode it to base to a, this is not really encoding, but mapping a char to a completely different one using
// the same order as encodeArray
private String encodeString(String s) {
Map<Character, Character> lookup = new HashMap<>();
int index = 0; // starting from 'a'
StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i); if (lookup.containsKey(c)) {
sb.append(lookup.get(c));
} else {
char t = (char)('a' + index);
sb.append(t);
index++;
lookup.put(c, t);
}
} return sb.toString();
}

Time Complexity: O(N), N is the length of pattern or string array, we loop it 3 times, but still O(N)
Space Complexity: O(N), N is the length of pattern or string array, we need the extra map and string to store the results

Use bijection mapping (Recommended)

 // I recommend this solution: just need map to keep the mapping relationship
public boolean wordPattern(String pattern, String str) {
if (pattern == null || pattern.length() == 0 || str == null || str.length() == 0) {
return false;
} String[] strs = str.trim().split(" "); if (pattern.length() != strs.length) {
return false;
} Map<Character, String> lookup = new HashMap<>();
// As it says, it is a bijection, so it needs to be 1 to 1 mapping, cannot exist a case one key maps to different value case
// E.g., need this set for abba, dog dog dog dog -> false case
Set<String> mapped = new HashSet<>(); for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i); if (lookup.containsKey(c)) {
if (!lookup.get(c).equals(strs[i])) {
return false;
}
} else {
// shit, just know put actually returns a V, which is the previous value, or null if not exist (or an associated null value)
lookup.put(c, strs[i]);
if (mapped.contains(strs[i])) {
return false;
}
mapped.add(strs[i]);
}
} return true;
}

There is also a clever implementation like below. The key point is use the index to compare, if there is duplicate index, meaning there are two keys already mapped to the same value. Also, remember java put() returns a valid, not a void :)

 // Reference: https://leetcode.com/problems/word-pattern/discuss/73402/8-lines-simple-Java
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length())
return false;
Map index = new HashMap();
for (Integer i=0; i<words.length; ++i)
if (index.put(pattern.charAt(i), i) != index.put(words[i], i))
return false;
return true;
}

Time Complexity: N is the length of pattern or string array
Space Complexity: O(N), N is the length of pattern or string array, we need a map regardless

References

Baozi Leetcode Solution 290: Word Pattern的更多相关文章

  1. 【leetcode】290. Word Pattern

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

  2. Leetcode solution 291: Word Pattern II

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

  3. 【一天一道LeetCode】#290. Word Pattern

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】290. Word Pattern 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

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

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

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

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

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

  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. OAUTH2 SAML2.0

    OAuth2 - http://www.cnblogs.com/linianhui/p/oauth2-authorization.html SAML - wikipedia Shibboleth / ...

  2. Delphi 7学习开发控件(继承TGraphicControl只画一条线)

    我们知道使用Delphi快速开发,很大的一方面就是其强大的VCL控件,另外丰富的第三方控件也使得Delphi程序员更加快速的开发出所需要的程序.在此不特别介绍一些概念,只记录自己学习开发控件的步骤.假 ...

  3. 使用PNG实现半透明的窗体(使用GDI+)

      Delphi中标准控件是不支持png图片的,据说从Window2000后增加gdiplus.dll库处理更多的gdi图像,其中包括png.   关键的几个api   GdipCreateBitma ...

  4. Java代码消除switch/case,if/else语句的几种实现方式

    转自:https://my.oschina.net/stefanzhlg/blog/372413 我们在平时的编码中,我们经常会遇到这样的情况: 使用过多的switch/case 或者 if else ...

  5. python中的内置函数(bytearray)

    返回一个新的字节数组.bytearray类是range 0 < = x < 256的一个可变序列.它有大多数可变序列的常用方法,在可变序列类型中描述,以及大多数字节类型的方法,参见字节和B ...

  6. pycharm安装及配置全过程

    首先要准备两个文件,一是pycharm-community,二是python-3.7.2-amd. PyCharm 的下载地址:http://www.jetbrains.com/pycharm/dow ...

  7. Django 你需要掌握的模型层(标签、过滤器、模板的继承与导入)

    Django 模型层(标签.过滤器.模板的继承与导入) 好文章来自超哥:https://www.cnblogs.com/guanchao/p/11006062.html   过滤器/自定义过滤器 模板 ...

  8. mysql数据库之表关系

    外键 前戏之一对多关系 # 定义一张部门员工表id name gender dep_name dep_desc1 jason male 教学部 教书育人2 egon male 外交部 漂泊游荡3 ta ...

  9. Python调试器-pdb的使用

    [简介] pdb是python自带的一个包,为python程序提供了一种交互的源代码调试功能. [使用方法] 1. 使用命令: python -m pdb xxx.py #可以直接进入单步执行模式 2 ...

  10. java基础知识总结(二)

    Java中的代码块 java中的代码块是用{}括起来的代码,进行一些功能的限定 静态代码块:在类第一次被初始化的是后执行,负责一些类的初始化操作,仅仅只执行一次 构造代码块:顾名思义,辅助构造器进行初 ...