码迷,mamicode.com
首页 > 其他好文 > 详细

Word Pattern | & II

时间:2016-07-30 00:28:49      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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.

 1     public boolean wordPattern(String pattern, String str) {
 2         String[] strs = str.split(" ");
 3         if (pattern.length() != strs.length) return false;
 4         Map<Character, String> map = new HashMap<Character, String>();
 5         for (int i = 0; i < pattern.length(); i++) {
 6             if (!map.containsKey(pattern.charAt(i))) {
 7                 if (map.containsValue(strs[i])) return false;
 8                 map.put(pattern.charAt(i), strs[i]);
 9             } else {
10                 if (!strs[i].equals(map.get(pattern.charAt(i)))) return false;
11             }
12         }
13         return true;
14     }

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. 

 1 public boolean wordPatternMatch(String pattern, String str) {
 2         return getMapping(pattern, str, new HashMap<Character, String>());
 3     }
 4 
 5     public boolean getMapping(String pattern, String str, HashMap<Character, String> mapping) {
 6         if (pattern.isEmpty() && str.isEmpty()) {
 7             return true;
 8         } else if (pattern.isEmpty() || str.isEmpty()) {
 9             return false;
10         }
11 
12         if (mapping.containsKey(pattern.charAt(0))) {
13             String map = mapping.get(pattern.charAt(0));
14             if (str.length() >= map.length() && str.substring(0, map.length()).equals(map)) {
15                 if (getMapping(pattern.substring(1), str.substring(map.length()), mapping)) {
16                     return true;
17                 }
18             }
19         } else {
20             for (int i = 1; i <= str.length(); i++) {  // try each pattern
21                 String p = str.substring(0, i);
22                 if (mapping.containsValue(p)) continue;
23                 mapping.put(pattern.charAt(0), p);
24                 if (getMapping(pattern.substring(1), str.substring(i), mapping)) {
25                     return true;
26                 }
27                 mapping.remove(pattern.charAt(0));
28             }
29         }
30         return false;
31     }

 

Word Pattern | & II

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5719953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!