Word Pattern
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 word in str.
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:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Think
- Firstly, you need to convert
str
into string array with spliting by space. - Check two stuffs are equal length, if not? directly return false!
- Setup a hashmap for storing element both pattern and strs. However, the key are different types: character, string. Why we do not just use string? Consider about this case: pattern -
abba
, str -a a b a
. Pattern and Word has the same kind of element. It cannot easily to distinguish. - The value in hashmap should be the
- There is one more tricky thing: why we compare the
map.put()
return value? Here is a segment in HashMap API.
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
- That means the previous index(value) of character(key) will be returned as index reference. So we return the previous index and also update the index for next index reference!
- So if the both returned index doesn't matched, it should return false.
Solution
public class Solution {
public boolean wordPattern(String pattern, String str) {
String[] strs = str.split(" ");
if(strs.length != pattern.length())
return false;
Map<Object, Integer> map = new HashMap<>();
for(int i = 0; i < strs.length; i++) {
if(!Objects.equals(map.put(pattern.charAt(i), i), map.put(strs[i], i)))
return false;
}
return true;
}
}
Problem 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.
Think
- Got all combinations and check any combination matched the pattern
Solution
public boolean wordPatternMatch(String pattern, String str) {
// get all combinations
List<String> combinations = new ArrayList<>();
backtracking(combinations, str, "", 0);
// check any matched case
boolean res = false;
for (String s : combinations) {
res |= wordPattern(pattern, s);
}
return res;
}
private void backtracking(List<String> combinations, String str,
String cur, int index) {
if (index >= str.length()) {
combinations.add(new String(cur));
return;
}
for (int i = index; i < str.length(); i++) {
String origin = cur;
backtracking(combinations, str, cur + (cur.length() == 0 ? "" : " ")
+ str.substring(index, i + 1), i + 1);
cur = origin;
}
}