题目:

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. QT对话框设计

    软件和系统:QTcreator5.7,win8.1 1. 首先新建项目,选择application中的Qt widgets application. 2. 创建类Dialog,选择QDialog作为基 ...

  2. TF-IDF与余弦相似性的应用(三):自动摘要

    有时候,很简单的数学方法,就可以完成很复杂的任务. 这个系列的前两部分就是很好的例子.仅仅依靠统计词频,就能找出关键词和相似文章.虽然它们算不上效果最好的方法,但肯定是最简便易行的方法. 今天,依然继 ...

  3. Jquery获取选中的checkbox的值

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  4. android应用程序的安装方式与原理

    android应用程序的安装方式与原理 四种安装方式: 1.系统应用安装――开机时完成,没有安装界面 2.网络下载应用安装――通过market应用完成,没有安装界面 3.ADB工具安装――没有安装界面 ...

  5. 2、onclickListener冲突

    事情是这样的. 我在activity中同时使用普通按钮和对话框按钮,并都设置点击时候的回调函数,由于都要用到onclickListener,但是两者却不是一个文件,无法同时import,这就是本文出现 ...

  6. Netsharp快速入门(之1) 介绍及需求说明

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 第一章 快速入门介绍 Netsharp是一个企业基础业务管理平台,介绍Netsharp分三个系列,分别是: 1.         N ...

  7. Ombrophobic Bovines - POJ 2391

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  8. ptype_base和ptype_all学习笔记

    "linux-2.6.32/include/linux/netdevice.h" struct packet_type { __be16 type; /* This is real ...

  9. Leetcode#145 Binary Tree Postorder Traversal

    原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...

  10. max_flow(Ford-Fulkerson) 分类: ACM TYPE 2014-09-02 01:50 110人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> #include<queue> usi ...