Word Pattern | & II

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;  // the upper if condition is its opposite
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     }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/5719953.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值