Word Pattern |

Given a pattern and a string str, find if str follows the same pattern.

Examples:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. pattern = "abba", str = "dog dog dog dog" should return false.

Notes:

  1. patterncontains only lowercase alphabetical letters, and str contains words separated by a single space. Each word in str contains only lowercase alphabetical letters.
  2. Both pattern and str do not have leading or trailing spaces.
  3. 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:

    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.

分析:

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的更多相关文章

  1. Word Pattern II 解答

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

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

  9. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

随机推荐

  1. C语言中memset(void *s, char ch,unsigned n)用的用法

    将指针s所指的内存空间中前n为重置为字符c 程序例: #include <string.h> #include <stdio.h> #include <memory.h& ...

  2. .net 使用PowerShell获取电脑中的UUID

    UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...

  3. BZOJ1001 狼抓兔子(裸网络流)

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  4. Linux中的TUN/TAP设备

    今天才发现这家伙...怎么讲...深以为耻.晚上的任务是加深对它的了解,就这么定了. 1. General questions.1.1 What is the TUN ?  The TUN is Vi ...

  5. 在Eclipse中编写servlet时出现"The import javax.servlet cannot be resolved" 问题解决办法

    在Eclipse中,project->properties,选择Java Build Path->Libraries->Add External JARs,找到你计算机中tomcat ...

  6. win2008r2激活码

    我这有三个 以前用过可以 现在不知道能不能用 你试试BBFP3-49FVF-TJB8F-V26V6-DJPX9 CXTFT-74V4Y-9D48T-2DMFW-TX7CYGYF3T-H2V88-GRP ...

  7. C语言时间函数

    #include "time.h" #include "stdio.h" #include "stdlib.h" int main() { ...

  8. (转)MFC的一些宏的整理 (DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE)

    很早看了MFC的一些宏的实现,什么RUNTIME_CLASS, DECLARE_DYNAMIC, DECLARE_DYNCREATE,IMPLEMENT_DYNCREATE, etc,看了就烦,现在整 ...

  9. 安装PL/SQL Developer 遇到的问题及解决方法

    在用PL/SQL Developer安装Oracle客户端时,报错误,初始化失败,一直找不到原因,换Oracle版本也解决不了问题,之后才发现,是Oracle的环境变量配置错了,之前用户配了Oracl ...

  10. 标准库errno.h 查看错误代码编号,errno:4 与error:2

    log 里报错,errno:4   与errno:2 查了一下 errno.h   --------下文来自百度百科 errno 编辑 errno 是记录系统的最后一次错误代码.代码是一个int型的值 ...