Word Pattern | & II
Word Pattern |
Given a pattern
and a string str
, find if str
follows the same pattern.
Examples:
- pattern =
"abba"
, str ="dog cat cat dog"
should return true. - pattern =
"abba"
, str ="dog cat cat fish"
should return false. - pattern =
"aaaa"
, str ="dog cat cat dog"
should return false. - pattern =
"abba"
, str ="dog dog dog dog"
should return false.
Notes:
pattern
contains only lowercase alphabetical letters, andstr
contains words separated by a single space. Each word instr
contains only lowercase alphabetical letters.- Both
pattern
andstr
do not have leading or trailing spaces. - Each letter in
pattern
must map to a word with length that is at least 1.
solution:
Split the string, and add the pair to hashmap, if the existing pattern in the hashmap doesn't match the current one, return false.
- public boolean wordPattern(String pattern, String str) {
- String[] strs = str.split(" ");
- if (pattern.length() != strs.length) return false;
- Map<Character, String> map = new HashMap<Character, String>();
- for (int i = ; i < pattern.length(); i++) {
- if (!map.containsKey(pattern.charAt(i))) {
- if (map.containsValue(strs[i])) return false;
- map.put(pattern.charAt(i), strs[i]);
- } else {
- if (!strs[i].equals(map.get(pattern.charAt(i)))) return false;
- }
- }
- return true;
- }
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.
- pattern =
Notes:
You may assume both pattern
and str
contains only lowercase letters.
分析:
As we don't know the breaking point in str, so we have to try one by one. Onc both the pattern string and str string are empty at the same time, it means the pattern we used is correct.
- public boolean wordPatternMatch(String pattern, String str) {
- return getMapping(pattern, str, new HashMap<Character, String>());
- }
- public boolean getMapping(String pattern, String str, HashMap<Character, String> mapping) {
- if (pattern.isEmpty() && str.isEmpty()) {
- return true;
- } else if (pattern.isEmpty() || str.isEmpty()) {
- return false;
- }
- if (mapping.containsKey(pattern.charAt())) {
- String map = mapping.get(pattern.charAt());
- if (str.length() >= map.length() && str.substring(, map.length()).equals(map)) {
- if (getMapping(pattern.substring(), str.substring(map.length()), mapping)) {
- return true;
- }
- }
- } else {
- for (int i = ; i <= str.length(); i++) { // try each pattern
- String p = str.substring(, i);
- if (mapping.containsValue(p)) continue; // the upper if condition is its opposite
- mapping.put(pattern.charAt(), p);
- if (getMapping(pattern.substring(), str.substring(i), mapping)) {
- return true;
- }
- mapping.remove(pattern.charAt());
- }
- }
- return false;
- }
Word Pattern | & II的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
随机推荐
- ORACLE建表练习
1,学生表 -- Create table create table T_HQ_XS ( xueh ) not null, xingm ) not null, xingb ) ', nianl NUM ...
- CentOS7 安装 Mono
官网参考:http://www.mono-project.com/docs/getting-started/install/linux/#centos-7-fedora-19-and-later-an ...
- 50行代码仿backbone_todos
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- hdu1588 矩阵快速幂
//看了很多的博客 后来队友指点才懂//sum=f(g(0))+f(g(1))+.... //sum=A^(b-1)*|...|.... //要将b-1换,防止出现b=0时有负一,用A^b代替,取下面 ...
- Java设计模式-观察者模式(Observer)
包括这个模式在内的接下来的四个模式,都是类和类之间的关系,不涉及到继承,学的时候应该 记得归纳,记得本文最开始的那个图.观察者模式很好理解,类似于邮件订阅和RSS订阅,当我们浏览一些博客或wiki时, ...
- Java虚拟机的功能
1:通过ClassLoader寻找和装载class文件 2:解释字节码成为指令并执行,提供class文件的运行环境.即将字节码转换为不同OS下可执行的机器码指令. 3:进行垃圾回收. 4:提供与硬件交 ...
- POJ1679The Unique MST(次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25203 Accepted: 8995 D ...
- 【转】KMP算法
转载请注明来源,并包含相关链接.http://www.cnblogs.com/yjiyjige/p/3263858.html 网上有很多讲解KMP算法的博客,我就不浪费时间再写一份了.直接推荐一个当初 ...
- spark-submit提示资源不足
ensure that workers are registered and have sufficient resources spark-cluster启动的配置里配置了每个worker的内存,如 ...
- vagrant 启动错误
Stderr: VBoxManage.EXE: error: Failed to create the VirtualBox object!VBoxManage.EXE: error: Code E_ ...