Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
Note:
You may assume both s and t have the same length.
For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
思路完全同290题:http://www.jianshu.com/p/4ffa25d11451
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(字符种类)
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。
Time Complexity: O(N) Space Complexity: O(字符种类)
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好
因为都是字符,共256中,3_c为将map替换为int数组做map
Time Complexity: O(N) Space Complexity: O(字符种类)
Solution1 Code:
public class Solution1 {
public boolean isIsomorphic(String s, String t) {
if(s == null || s.length() <= 1) return true;
Map<Character, Character> map_forw = new HashMap();
Map<Character, Character> map_bacw = new HashMap();
for (int i = 0; i < s.length(); ++i) {
if(!map_forw.containsKey(s.charAt(i)) && !map_bacw.containsKey(t.charAt(i))) {
map_forw.put(s.charAt(i), t.charAt(i));
map_bacw.put(t.charAt(i), s.charAt(i));
}
else if(!map_bacw.containsKey(t.charAt(i)) || !map_bacw.get(t.charAt(i)).equals(s.charAt(i))) {
return false;
}
}
return true;
}
}
Solution2 Code:
public class Solution2 {
public boolean isIsomorphic(String s, String t) {
if(s == null || s.length() <= 1) return true;
Map<Character, Character> map_forw = new HashMap();
Set<Character> back_set = new HashSet();
for (int i = 0; i < s.length(); ++i) {
if(!map_forw.containsKey(s.charAt(i)) && !back_set.contains(t.charAt(i))) {
map_forw.put(s.charAt(i), t.charAt(i));
back_set.add(t.charAt(i));
}
else if(!map_forw.containsKey(s.charAt(i)) || !map_forw.get(s.charAt(i)).equals(t.charAt(i))) {
return false;
}
}
return true;
}
}
Solution3_a Code:
public class Solution {
public boolean isIsomorphic(String s1, String s2) {
HashMap<Character, Integer> map1 = new HashMap<Character, Integer>();
HashMap<Character, Integer> map2 = new HashMap<Character, Integer>();
for (Integer i = 0; i < s1.length(); i++) {
if (map1.get(s1.charAt(i)) != map2.get(s2.charAt(i))) return false;
map1.put(s1.charAt(i), i);
map2.put(s2.charAt(i), i);
}
return true;
}
}
Solution3_b Code:
public class Solution {
public boolean isIsomorphic(String s1, String s2) {
HashMap<Character, Integer> map1 = new HashMap<Character, Integer>();
HashMap<Character, Integer> map2 = new HashMap<Character, Integer>();
for (int i = 0; i < s1.length(); i++) {
if(!map1.containsKey(s1.charAt(i)) ^ !map2.containsKey(s2.charAt(i))) return false;
if (map1.get(s1.charAt(i)) != null && !map1.get(s1.charAt(i)).equals(map2.get(s2.charAt(i)))) return false;
map1.put(s1.charAt(i), i);
map2.put(s2.charAt(i), i);
}
return true;
}
}
Solution3_c Code:
public class Solution {
public boolean isIsomorphic(String s1, String s2) {
int[] map1 = new int[256];
int[] map2 = new int[256];
for (int i = 0; i < s1.length(); i++) {
if(map1[s1.charAt(i)] != map2[s2.charAt(i)]) return false;
map1[s1.charAt(i)] = map2[s2.charAt(i)] = i + 1; //avoid 0 when inited
}
return true;
}
}