Word Pattern II 解答
Question
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.
Solution
Word Pattern 是用HashMap的containsKey()和containsValue()两个方法实现的。
但是对于Word Pattern II,我们并不知道怎样划分str,所以基本想法是“暴力搜索”即backtracking/DFS。这里DFS的存储对象不是常见的list,而是map.
public class Solution {
public boolean wordPatternMatch(String pattern, String str) {
return helper(pattern, 0, str, 0, new HashMap<Character, String>());
}
private boolean helper(String pattern, int startP, String str, int startS, Map<Character, String> map) {
if (startP == pattern.length() && startS == str.length()) {
return true;
} else if (startP == pattern.length()) {
}
if (match.equals(str.substring(startS, endS))) {
return helper(pattern, startP + 1, str, endS, map);
} else {
return false;
}
} else {
// If map does not have existing key
// Traverse, brute force
for (int i = startS + 1; i <= str.length(); i++) {
String candidate = str.substring(startS, i);
if (map.containsValue(candidate)) {
continue;
}
map.put(key, candidate);
if (helper(pattern, startP + 1, str, i, map)) {
return true;
}
map.remove(key);
}
}
return false;
}
}
Word Pattern II 解答的更多相关文章
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- 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 ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- 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 ...
- Leetcode: Word Pattern II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- Word Break II 解答
Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...
- Word Search II 解答
Question Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
随机推荐
- 什么是空间复杂度(What is actually Space Complexity ?)
属于空间复杂度(Space Complexity)在很多情况下被错认为是附属空间(Auxiliary Space),下面是附属空间和空间复杂度的定义. 附属空间(Auxiliary Space)是算法 ...
- python使用deque实现fifo(先进先出)
#2:deque还提供了append 和pop方法,在序列的两端都能以相同的速度执行#例子2:使用deque实现fifo(先进先出),如下:from collections import dequei ...
- matlab遗传算法
function [xv,fv] = myGA(fitness, a, b, NP, NG, Pc, Pm, eps) % 用遗传算法求解一维无约束优化问题 % % 待优化的目标函数 fitness ...
- Android开发常用工具汇总
Android开发常用工具汇总,本文章不断更新完善 一.数据库小工具Sqlite Developer SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它的设计目标是嵌入式的, ...
- Eclipse+Java+OpenCV246人脸识别
1.环境搭建:见上一篇博客 整个项目的结构图: 2.编写DetectFaceDemo.java,代码如下: package com.njupt.zhb.test; import org.opencv. ...
- [manacher] hdu 3294 Girls' research
题意: 给一个字符x代表真实的a 然后输出的时候转换 然后就是求最长回文子串的串是什么 长度要大于1 思路: 就是裸的manacher,弄清楚下标的转换关系就好了 代码: #include" ...
- 利用JS实现简单的瀑布流效果
哈哈, 我又来啦, 在这一段时间里, 我简单的学习了一下javascript(JS), 虽然不是很懂啦, 但是我也简单的尝试着做了点小东西, 就比如现在流行的瀑布流效果, 经过我的努力终于成功的完成了 ...
- Linux查看网络即时网速
sar -n DEV 1 100 1代表一秒统计并显示一次 100代表统计一百次 使用ntop等工具,就更方便简单了,不过这个更灵活 P.S. sar在sysstat包 来源:http://www.c ...
- ios按钮点击时的灰色框
a,button,input,textarea{-webkit-tap-highlight-color: rgba(0,0,0,0;)}
- NFinal学习笔记(一)
NFinal框架,最快的netWeb框架,其有三大特色,NFinalServer,NFinalBuild,NFinal代码生成器(生成web层) 首先学习了NFinalServer. NFinalSe ...