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:
patterncontains only lowercase alphabetical letters, andstrcontains words separated by a single space. Each word instrcontains only lowercase alphabetical letters.- Both
patternandstrdo not have leading or trailing spaces. - Each letter in
patternmust 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:
- 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.
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 }

浙公网安备 33010602011771号