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:
pattern = "abab", str = "redblueredblue" should return true.
pattern = "aaaa", str = "asdasdasdasd" should return true.
pattern = "aabb", str = "xyzabcxzyabc" should return false.
Notes:
You may assume both pattern and str contains only lowercase letters.

因为目标字符串可以任意划分,所以我们不得不尝试所有可能性。这里通过深度优先搜索的回溯法,对于pattern中每个字母,在str中尝试所有的划分方式,如果划分出来的子串可以用这个字母映射,或者可以建立一个新的字母和字符串的映射关系,我们就继续递归判断下一个pattern中的字母,直到pattern的指针和str的指针同时指到最末

技巧在于,

1. 用HashMap + HashSet来保证一一对应

2. 把HashMap和HashSet用作instance variable, 甚至boolean result也可用作instance variable, 这样任何在递归调用函数里所做的改变,都会保留,而不用把HashMap, HashSet, result加入递归argument

 public class Solution {
HashMap<Character, String> map = new HashMap<Character, String>();
HashSet<String> set = new HashSet<String>(); public boolean wordPatternMatch(String pattern, String str) {
if (pattern==null || str==null) return false;
return helper(pattern, str, 0, 0);
} public boolean helper(String pattern, String str, int i, int j) {
if (i==pattern.length() && j==str.length()) {
return true;
}
if (i==pattern.length() || j==str.length()) return false;
char key = pattern.charAt(i);
for (int cut=j+1; cut<=str.length(); cut++) {
String trial = str.substring(j, cut);
if (!map.containsKey(key) && !set.contains(trial)) { // ensure one-on-one matching
map.put(key, trial);
set.add(trial);
if (helper(pattern, str, i+1, cut))
return true;
map.remove(key);
set.remove(trial);
}
else if (map.containsKey(key) && map.get(key).equals(trial)) {
if (helper(pattern, str, i+1, cut))
return true;
}
}
return false;
}
}

Leetcode: Word Pattern II的更多相关文章

  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 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 、lintcode 829. Word Pattern II

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

  4. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  5. Word Pattern | & II

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

  6. Word Pattern II 解答

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

  7. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  8. [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 ...

  9. 291. Word Pattern II

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

随机推荐

  1. 【转】设计模式(三)建造者模式Builder(创建型)

    (http://blog.csdn.net/hguisu/article/details/7518060) 1. 概述 在软件开发的过程中,当遇到一个"复杂的对象"的创建工作,该对 ...

  2. HTML5 本地存储 LocalStorage

    说到本地存储,这玩意真是历尽千辛万苦才走到HTML5这一步,之前的历史大概如下图所示: 最早的Cookies自然是大家都知道,问题主要就是太小,大概也就4KB的样子,而且IE6只支持每个域名20个co ...

  3. Qt 窗口属性简介之Qt::WA_DeleteOnClose

    一.简述 今天介绍一个简单的窗口属性——Qt::WA_DeleteOnClose. 在正常创建窗口后,我们一般会调用close()方法来关闭窗口,这里我们看一下Q助手中关于close()方法的介绍. ...

  4. DevExpress的所有功能介绍

    https://www.devexpress.com/Subscriptions/New-2016-2.xml?utm_source=AnnounceTry&utm_medium=WhatsN ...

  5. Cocos2d-JS工程中的文件结构

    res文件夹存放资源文件 src文件夹是主要的程序代码 app.js是实现游戏场景的JavaScript文件 resource.js在src文件夹中,定义资源对应的变量 config.json保存模拟 ...

  6. sqlserver 中含有某字符串

    查找 sqlserver 中字符串的ascii码SET TEXTSIZE 0-- Create variables for the character string and for the curre ...

  7. Essential Documents to Manage Your Projects

    Speak to an experienced project manager, and they can give you a wealth of good advice on the do's a ...

  8. Java学习-026-类名或方法名应用之二 -- 统计分析基础

    前文讲述了类名或方法的应用之一调试源码,具体请参阅:Java学习-025-类名或方法名应用之一 -- 调试源码 此文主要讲述类名或方法应用之二统计分析,通过在各个方法中插桩(调用桩方法),获取方法的调 ...

  9. maven小记

    编译webx3.0,必须用maven 3.0 git clone https://github.com/webx/citrus.gitmaven clean install -DskipTest(Te ...

  10. linux spi 设备节点 读写

    本文记录spi设备节点的操作方法. SPI总线设备文件名通常为/dev/spidevN.P(N=0.1.2--,P=0.1.2--), 其中N表示第几路SPI总线,而P表示在该路SPI总线中使用哪个C ...