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:

  1. pattern = "abab", str = "redblueredblue" should return true.
  2. pattern = "aaaa", str = "asdasdasdasd" should return true.
  3. 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 解答的更多相关文章

  1. Word Pattern | & II

    Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...

  2. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

  3. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  4. 291. Word Pattern II

    题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...

  5. Leetcode solution 291: Word Pattern II

    Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...

  6. [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 ...

  7. Leetcode: Word Pattern II

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  8. Word Break II 解答

    Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...

  9. Word Search II 解答

    Question Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

随机推荐

  1. HDU4453--Looploop (Splay伸展树)

    Looploop XXX gets a new toy named Looploop. The toy has N elements arranged in a loop, an arrow poin ...

  2. poj1284:欧拉函数+原根

    何为原根?由费马小定理可知 如果a于p互质 则有a^(p-1)≡1(mod p)对于任意的a是不是一定要到p-1次幂才会出现上述情况呢?显然不是,当第一次出现a^k≡1(mod p)时, 记为ep(a ...

  3. 在 Ubuntu 12.04 上安装 GitLab6.0

    安装环境: 操作系统:    Ubuntu 12.4 LTS 英文 数据库:        mysql5.5.32 web服务器: nginx1.4.1 首先, 添加git和nginx的ppa,并升级 ...

  4. windows 运行打开服务命令

    转载自:http://www.2cto.com/os/201209/157464.html windows运行打开服务命令 Java代码  1. gpedit.msc-----组策略  2. sndr ...

  5. [Cycle.js] Fine-grained control over the DOM Source

    Currently in our main() function,  we get click$ event. function main(sources) { const click$ = sour ...

  6. Java中日期时间API小结

    Java中为处理日期和时间提供了大量的API,确实有把一件简单的事情搞复杂的嫌疑,各种类:Date Time Timestamp Calendar...,但是如果能够看到时间处理的本质就可以轻松hol ...

  7. javascript 模仿 html5 placeholder

    <form action="?action=deliver" method="post" class="deliver-form"&g ...

  8. 配置错误--分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。

    在部署网站的时候,很容易遇到这个一样错误:分析器错误消息: 无法识别的属性“targetFramework”.请注意属性名称区分大小写.  错误如图: 错误原因: 部署网站时,使用的应用程序池版本不对 ...

  9. unity针对iphone的屏幕旋转

    屏幕旋转可以在引擎里设置: 依次点开 Edit——Project Setting——Player 即可设置如图: 接下来的是 雨松大神的 代码控制,本屌是安卓机器,没能测试. C# using Uni ...

  10. nyoj 最少步数

    算法:搜索(深度优先搜索) 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1, ...