Leetcode: Word Pattern II
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的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- Leetcode solution 291: Word Pattern II
Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [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 ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
随机推荐
- sshd调优
sshd调优:禁用dns查找,加快速度在sshd_config中设置:UseDNS no禁用root登录:建立普通用户在sshd_config中设置PermitRootLogin no以上设置需要重启 ...
- MP20 MBO issue summary
MP3 MBO经验,教训 改名字HGA003_PTOT_01,发现居然闪现进度条,正常情况是不会闪现进度条的,只是改个名字而已,怀疑之前用过这个名字,所以我后来改成HGA003_PTOT_03了. 先 ...
- Delphi 中的哈希表(二)—— TStringHash
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 【php学习】mysql数据库操作
//建立数据库连接,数据库地址127.0.0,用户名root,密码root $dbConn = mysql_connect('127.0.0.1', 'root', 'root'); mysql_qu ...
- CC2540 USB Dongle 使用说明
CC2540做的USB Dongle可以烧写不同的固件从而做很多PC端的应用,下面我们来介绍下下载固件的方法和一些典型应用: 下载接口: 3V3引脚连接到CC Debugger的Target Volt ...
- IOS常见的三种回调方法介绍
认识下三种IOS常见的回调模式. 代理模式作为IOS中最常见的通讯模式,代理几乎无处不在. 这里有一个数组,我们首先通过代理的方式将数组传递到其他方法中去. 设置协议及方法 @protocol Cal ...
- membership db注册工具
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
- eclipse根据.wsdl文件自动生成webservice的调用客户端
1.工具:eclipse3.3或者是带有webservice插件的eclipse 2. 首先用浏览器访问webservice的站点,接着保存打开的页面,后缀为.wsdl. 3.把保存好的文件拷入ecl ...
- 简易自定义下拉菜单 与简易默认下拉html片段
简易自定义下拉选择 html片段 html: <div class="select_box province"> <div class="selecte ...
- [LeetCode]题解(python):035-Search Insert Position
题目来源 https://leetcode.com/problems/search-insert-position/ Given a sorted array and a target value, ...