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.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
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.
思路完全同205题:http://www.jianshu.com/p/6e03ef397be1
Solution1:two hashmap(forward mapping & backward mapping)
思路:"a b c", "x y x": a -> x, b -> y, c -> z; x -> a, y -> b, z -> c
前向mapping,和 后向mapping 都要有。
前向避免:a -> x, a -> y的情况,后向避免a->x, b->x的情况。
(1) map_bacw.get(t.charAt(i)) == s.charAt(i)
(2) map_forw.get(s.charAt(i)) == t.charAt(i)
其实只check一个就行,Solution2中说明
Time Complexity: O(N) Space Complexity: O(N)
Solution2:1 hashmap 1 hashset (forward mapping & "back" set)
(1)map_bacw.get(t.charAt(i)) == s.charAt(i)
(2)map_forw.get(s.charAt(i)) == t.charAt(i)
思路: 但其实不需要前向后向都map,因为a: x代表ax是一组的,只check a是否->x即可,因为如果a->x,x也肯定是和a一组的,因为只有在刚开始两者都不存在的时候在会建组(map),就是说如果其中一个是有组的,就不会再建(不会和别的再组)。所以一个map即可,另一个反向的map只是为了表示是contain的,就是表示原来是出现过的有组了的。
所以可以两个hashmap,最后只检查一个条件(1)或(2)即可,或者直接将一个该为hashset。
Solution3:2 hashmap mapping element -> recent index
思路:相当于用index作为中间变量来表示他们是一组。
Note:实现时 如果把Integer改为int就不对了,因为map是<Object, Object>,如果传入int时会自动转成Integer。所以如果用int的话,两个int分别转为两个Integer,这样用!=判断的时候其实就当成了不同的变量return false (而不是判断实际值)。所以如果用int的话,需要用!...equals()来判断实际值,但不能null.equals(),所以还有判断取出的object是非null,如3_b所示,所以还是直接用Integer好
Time Complexity: O(N) Space Complexity: O(N)
Solution1 Code:
class Solution1 {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length()) return false;
Map<Character, String> map_forw = new HashMap();
Map<String, Character> map_bacw = new HashMap();
for (int i = 0; i < pattern.length(); ++i) {
if(!map_forw.containsKey(pattern.charAt(i)) && !map_bacw.containsKey(words[i])) {
map_forw.put(pattern.charAt(i), words[i]);
map_bacw.put(words[i], pattern.charAt(i));
}
else if(!map_bacw.containsKey(words[i]) || !map_bacw.get(words[i]).equals(pattern.charAt(i))) {
return false;
}
}
return true;
}
}
Solution2 Code:
class Solution {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length()) return false;
Map<Character, String> map_forw = new HashMap();
Set<String> back_set = new HashSet();
for (int i = 0; i < pattern.length(); ++i) {
System.out.println(i);
if(!map_forw.containsKey(pattern.charAt(i)) && !back_set.contains(words[i])) {
map_forw.put(pattern.charAt(i), words[i]);
back_set.add(words[i]);
}
else if(!map_forw.containsKey(pattern.charAt(i)) || !map_forw.get(pattern.charAt(i)).equals(words[i])) {
return false;
}
}
return true;
}
}
Solution3_a Code:
class Solution {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length()) return false;
HashMap<Character, Integer> map1 = new HashMap<Character, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
for (Integer i = 0; i < pattern.length(); i++) {
if (map1.get(pattern.charAt(i)) != map2.get(words[i])) return false;
map1.put(pattern.charAt(i), i);
map2.put(words[i], i);
}
return true;
}
}
Solution3_b Code:
class Solution {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length()) return false;
HashMap<Character, Integer> map1 = new HashMap<Character, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
for (int i = 0; i < pattern.length(); i++) {
if(!map1.containsKey(pattern.charAt(i)) ^ !map2.containsKey(words[i])) return false;
if(map1.get(pattern.charAt(i)) != null && !map1.get(pattern.charAt(i)).equals(map2.get(words[i]))) return false;
map1.put(pattern.charAt(i), i);
map2.put(words[i], i);
}
return true;
}
}