291. 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.
链接: 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的更多相关文章
- 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] 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 ...
- 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 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- [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: Word Pattern II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [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 ...
- 290. Word Pattern 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
随机推荐
- cocos2dx 3.x中的渲染机制
1.由2.x的渲染节点,变成添加渲染命令,可以避免重复渲染相同的节点,提高了渲染效率 2.单机游戏通常要求apk包在30M以内,没压缩1M会有1%的转换率(下载转换率),即收入会提高 3.2.x中首先 ...
- C语言中链表节点的实现,以及如何实现泛型
1.C语言中的struct是纯粹的结构体,没有访问权限的概念 2.C语言中用void* 来实现泛型编程,也是C++类和模板底层实现的基础,就是用void*来实现的 #include<stdio. ...
- 四则运算2--c++
1.设计思路: 上篇已写,不在解释..... 2.源代码: #include<iostream.h>#include<stdlib.h>#include "time. ...
- 玩耍Hibernate系列(一)--基础知识
Hibernate框架介绍: Hibernate ORM 主要用于持久化对象(最常用的框架) Hibernate Search 用于对对象进行搜索,底层基于Apache Lucene做的 Hib ...
- php对图片反色处理
今天有个需求用php对图片进行反色,和转灰,之前不知道可不可行,后来看到了imagefilter()函数,用来转灰绰绰有余,好强大: imagefilter($im, IMG_FILTER_GRAYS ...
- .NET Framework 4.5、4.5.1 和 4.5.2 中的新增功能
.NET Framework 4.5.4.5.1 和 4.5.2 中的新增功能 https://msdn.microsoft.com/zh-cn/library/ms171868.aspx
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
- Leetcode#146 LRU Cache
原题地址 以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时.通常大家都是用map+双向链表做的. 我曾经尝试用C++的list容器来写,后来发现map没法保存lis ...
- Leetcode#172 Fractorial Trailing Zero
原题地址 n!含有多少个因子10,则结尾有多少个0 10=2*5,而2的个数肯定比5多,所以n!含有多少个因子5,则结尾有多少个0 如何计算n!有多少个因子5呢? 比如n=13,则: n! = 13 ...
- NF3 里面的z cull reverse reload
nf3 siggraph2011的 分享 里面有谈对csm的优化. 1.mask white red 2. HI Z 这俩我都懂 3.reverse depth buffer这实在不明白, 为什么会有 ...