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. JMS相关概念

    1.相关概念 1)JMS jms即Java消息服务(Java Message Service) 是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息 ...

  2. Sphinx+MySQL5.1x+SphinxSE+mmseg

    一.不停止mysql的情况下安装SphinxSE 1.确定mysql版本,下载对应源码包 此处下载5.1.69的mysql源码包 #wget ftp://ftp.ntu.edu.tw/pub/MySQ ...

  3. LVS的DR模式配置

    一.基本规划负载均衡调度器    192.168.1.104    默认网关    192.168.1.1    ip别名    192.168.1.233realserver1    192.168 ...

  4. 【转】Cocos2d - 观察者模式NotificationCenter

    http://shahdza.blog.51cto.com/2410787/1611575 [唠叨] 观察者模式 也叫订阅/发布(Subscribe/Publish)模式,是 MVC( 模型-视图-控 ...

  5. Xlib 窗口属性

    Xlib 窗口属性 转, 无法找到原作者 所有的 InputOutput 窗口都可以有零个或者多个像素的边框宽度,一个可选的背景,一个事件压制掩码(它压制来自孩子的事件传播),和一个 property ...

  6. Python的安装

    篇幅主要是别人的分享,我这里主要是添加注意点.我当初就是按照下面的图片开始安装python,安装的是python3.5,pyDev也是使用的博主的(还花了1资源分下载).但是运行程序时,一直显示 Er ...

  7. CC2540 USB Dongle 使用说明

    CC2540做的USB Dongle可以烧写不同的固件从而做很多PC端的应用,下面我们来介绍下下载固件的方法和一些典型应用: 下载接口: 3V3引脚连接到CC Debugger的Target Volt ...

  8. 规则html表单对象赋值

    function grid_load_callback(data, status) {            if (data.rows.length > 0)            {     ...

  9. oracle变量的定义和使用【转】

    在程序中定义变量.常量和参数时,则必须要为它们指定PL/SQL数据类型.在编写PL/SQL程序时,可以使用标量(Scalar)类型.复合(Composite)类型.参照(Reference)类型和LO ...

  10. JSON.stringify实例应用—将对象转换成JSON类型进行AJAX异步传值

    在上一篇中,对JSON.stringify()方法有了初步的认识,并且做了一些简单的例子.本篇将进一步将JSON.stringify用在复杂些的实例中,例如如下需求: 在进jQuery AJAX异步传 ...