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 ...
随机推荐
- ThinkPHP运算符与PHP运算符对照表
ThinkPHP运算符与PHP运算符对照表 ThinkPHP标签 说明及对应PHP标签 备注 eq 等于(=)(==:用于模板判断时) 可用于查询条件与模板判断 neq 不等于(!=) 可用于查询条件 ...
- Daily Scrum3
今天我们小组开会内容分为以下部分: part 1: 汇报之前分配的任务进度: part 2:分配明天的任务. ◆Part 1 组员进度报告 彭佟小组完成的优化目标: 关于软件防滥用及垃圾信息拦 ...
- 【每日scrum】NO.6
Yesterday:组内各种乱八七糟的问题,还有自己的效率问题 Today:进行小范围的测试实验 Problem:在显示各景点构成的邻接矩阵的时候,第一次编译未出现任何错误的提示,但是在程序运行时,无 ...
- hive搭建配置
下载cd /data0/software/hivewget http://mirror.bit.edu.cn/apache/hive/hive-0.12.0/hive-0.12.0-bin.tar.g ...
- mysql字符集基础知识梳理
接着上一篇继续来一篇关于mysql字符设置等问题学习笔记,这篇就不说什么废话了,直接进入正题,不过还是感谢十八哥的无私分享! 我们首先看看mysql整个数据存储和读取一个流程: 连接器(connect ...
- C#外挂QQ找茬辅助源码,早期开发
这是一款几年前开发的工具,当年作为一民IT纯屌,为了当年自己心目中的一位女神熬夜开发完成.女神使用后找茬等级瞬间从眼明手快升级为三只眼...每次看到这个就会想起那段屌丝与女神的回忆.今天特地把代码更新 ...
- Netsharp快速入门(之9) 基础档案(工作区3 添加商品菜单,以及在产品中打开商品界面)
作者:秋时 杨昶 时间:2014-02-15 转载须说明出处 3.5.2 添加导航菜单 1.打开平台工具,插件和资源节点,选择创建导航菜单,打开创建向导 2.选择所属插件 3.选择在哪个分类下 ...
- SQL Server性能优化(5)表设计时的注意事项
一. 是否需要冗余列 现在一些项目的数据库设计中,为了提高查询速度,把基本表的一些列也放到了数据表里,导致数据冗余.例如在热表的数据库里,原始数据表Measure_Heat里加了如房间号,单元号,楼号 ...
- 使用python远程登录
最近要使用python做一个在web上管理交换机的程序,需要远程登录,就查了点资料,由于还没有搞到交换机,就先用自己的机器测试一下. 首先python的标准库中包含telnet,用起来也很方便,查看一 ...
- Retry Pattern
Retry Pattern https://msdn.microsoft.com/en-us/library/dn589788.aspx https://msdn.microsoft.com/en-u ...