题目:

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 substring in str.

Examples:

  1. pattern = "abab", str = "redblueredblue" should return true.
  2. pattern = "aaaa", str = "asdasdasdasd" should return true.
  3. pattern = "aabb", str = "xyzabcxzyabc" should return false.

Notes:
You may assume both pattern and str contains only lowercase letters.

链接:  http://leetcode.com/problems/word-pattern-ii/

题解:

题目跟Word Pattern基本一样,但输入str里面没有delimiter,所以我们要使用Backtracking来做。因为上一题使用了两个map,所以这一题延续使用,结果给自己造成了很大的困难,代码也写的长而难懂。二刷一定要好好研究backtracking,争取也写出简洁漂亮的代码。

Time Complexity - O(2n), Space Complexity - O(2n)

public class Solution {
public boolean wordPatternMatch(String pattern, String str) {
if(pattern.length() == 0 && str.length() == 0) {
return true;
}
if(pattern.length() == 0 || str.length() == 0) {
return false;
}
Map<Character, String> patternToStr = new HashMap<>();
Map<String, Character> strToPattern = new HashMap<>();
return wordPatternMatch(pattern, str, patternToStr, strToPattern, 0, 0);
} private boolean wordPatternMatch(String pattern, String str, Map<Character, String> patternToStr, Map<String, Character> strToPattern, int posPattern, int posString) {
if(posPattern == pattern.length()) {
return true;
}
if(str.length() == 0 && posPattern < pattern.length()) {
return false;
} int i = 0;
for(i = posString; i < str.length(); i++) {
String word = str.substring(posString, i + 1);
if(posPattern >= pattern.length()) {
return false;
}
char c = pattern.charAt(posPattern); if(!patternToStr.containsKey(c) && !strToPattern.containsKey(word)) {
patternToStr.put(c, word);
strToPattern.put(word, c);
if(wordPatternMatch(pattern, str.substring(i + 1), patternToStr, strToPattern, posPattern + 1, 0)) {
return true;
}
patternToStr.remove(c);
strToPattern.remove(word);
} else if(patternToStr.containsKey(c) && !word.equals(patternToStr.get(c))) {
if(word.length() == patternToStr.get(c).length()) {
return false;
}
} else if(strToPattern.containsKey(word) && c != strToPattern.get(word)) {
} else {
posPattern++;
posString += word.length();
}
} return posPattern == pattern.length() ? true: false;
}
}

Reference:

https://leetcode.com/discuss/63252/share-my-java-backtracking-solution

https://leetcode.com/discuss/63393/20-lines-java-clean-solution-easy-to-understand

https://leetcode.com/discuss/63583/20-lines-concise-java-solution-with-explanation

https://leetcode.com/discuss/63724/super-easy-understand-backtracking-java-solution

291. Word Pattern II的更多相关文章

  1. Leetcode solution 291: Word Pattern II

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

  2. [LeetCode] 291. Word Pattern II 词语模式 II

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

  3. Word Pattern | & II

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

  4. Word Pattern II 解答

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

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

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

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

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

  7. Leetcode: Word Pattern II

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

  8. [Swift]LeetCode291. 单词模式 II $ Word Pattern II

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

  9. 290. Word Pattern 单词匹配模式

    [抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

随机推荐

  1. 二、freemarker.controller半自动静态化+Tomcat虚拟资源映射

    描述:本内容主要是讲2个tomcat之间同时共享一个静态话页面,统一入口是springMVC的一个controller,静态化的更新只需要传false.true.把完成的web项目放入a.b服务器To ...

  2. 不会JS中的OOP,你也太菜了吧!(第一篇)

    一.你必须知道的 1) 字面量 2) 原型 3) 原型链 4) 构造函数 5) 稳妥对象(没有公共属性,而且其方法也不引用this的对象.稳妥对象适合用在安全的环境中和防止数据被其它程序改变的时候) ...

  3. 工作点滴积累(1)---MD5和编码

    今天同事忽然问了一个问题,他发现同一个字符,比如"电影",用项目中的md5工具类生成的hash值和网上提供的在线MD5生成的hash值有时不一样,在只包含了字母字符串中,生成的ha ...

  4. poj 3264 Balanced Lineup 区间极值RMQ

    题目链接:http://poj.org/problem?id=3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) alw ...

  5. JavaScript之substring()方法讲解

    定义和用法 substring() 方法用于提取字符串中介于两个指定下标之间的字符. 语法 stringObject.substring(start,stop) 参数 描述 start 必需.一个非负 ...

  6. volatile关键字的使用

    (简要概括:volatile变量有两个作用:一个是告诉编译器不要进行优化:另一个是告诉系统始终从内存中取变量的地址,而不是从缓存中取变量的值) 一.前言 1.编译器优化介绍: 由于内存访问速度远不及C ...

  7. Javascript在页面加载时的执行顺序【转】

    一.在HTML中嵌入Javasript的方法 直接在Javascript代码放在标记对<script>和</script>之间 由<script />标记的src属 ...

  8. 使用CSS3实现超炫的Loading(加载)动画效果

    SpinKit 是一套网页动画效果,包含8种基于 CSS3 实现的很炫的加载动画.借助 CSS3 Animation 的强大功能来创建平滑,易于定制的动画.SpinKit 的目标不是提供一个每个浏览器 ...

  9. mybatis sql注入安全

    1.mybatis语句 SELECT * FROM console_operator WHERE login_name=#{loginName} AND login_pwd=#{loginPwd} 2 ...

  10. 超快的 FastText

    Word2Vec 作者.脸书科学家 Mikolov 文本分类新作 fastText:方法简单,号称并不需要深度学习那样几小时或者几天的训练时间,在普通 CPU 上最快几十秒就可以训练模型,得到不错的结 ...