LeetCode Word Pattern II
原题链接在这里:https://leetcode.com/problems/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.
Notes:
You may assume both pattern
and str
contains only lowercase letters.
题解:
类似Word Pattern. 但这里没有明确分割words的空格.
采用backtracking, 用pattern内的一个char来试着match不同长度的str substring, 能同时试到pattern 和 str的结尾, 就返回true.
e.g. pattern = "abab", str = "redblueredblue", 先用"a" match "r", "b" match "e", 又遇到"a", 但str里确实"d", 不match.
就backtracking下然后用"b" match "ed", 以此类推.
用HashMap<Character, String> 来保存已经走过的pattern char 和 str substring对应关系.
同时maintain一个set用来避免相同的string 来match 不同的char. e.g. "a" match "cde", "b"也match "cde".
Time Complexity: exponential. Space: O(hm.size() + hs.size()).
AC Java:
1 public class Solution { 2 public boolean wordPatternMatch(String pattern, String str) { 3 HashMap<Character, String> hm = new HashMap<Character, String>(); 4 HashSet<String> hs = new HashSet<String>(); 5 return isMatch(pattern, 0, str, 0, hm, hs); 6 } 7 8 private boolean isMatch(String pattern, int i, String str, int j, HashMap<Character, String> hm, HashSet<String> hs){ 9 //stop condition 10 if(i == pattern.length() && j == str.length()){ 11 return true; 12 } 13 if(i == pattern.length() || j == str.length()){ 14 return false; 15 } 16 17 char c = pattern.charAt(i); 18 19 //If current pattern character already exists in map 20 if(hm.containsKey(c)){ 21 String cString = hm.get(c); 22 23 //If corresponding string does not match str.substring(j, j+matchedString.length()). 24 if(!str.startsWith(cString, j)){ 25 return false; 26 } 27 return isMatch(pattern, i+1, str, j+cString.length(), hm, hs); 28 }else{ 29 //current pattern character doesn't exist in the map 30 for(int k = j; k<str.length(); k++){ 31 String s = str.substring(j, k+1); 32 33 //different strings can't be matched to same char 34 if(hs.contains(s)){ 35 continue; 36 } 37 38 hm.put(c, s); 39 hs.add(s); 40 41 //if all reset string could be matched 42 if(isMatch(pattern, i+1, str, k+1, hm, hs)){ 43 return true; 44 } 45 46 hm.remove(c); 47 hs.remove(s); 48 } 49 50 //Have tried all different lengths 51 return false; 52 } 53 } 54 }