Given two strings s and t, determine if they are isomorphic.java
Two strings are isomorphic if the characters in s can be replaced to get t.git
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.github
Example 1:code
Input: s = t = Output: true"egg","add"
Example 2:leetcode
Input: s = t = Output: false"foo","bar"
Example 3:get
Input: s = t = Output: true"paper","title"
// 這裏我維護兩個 map來處理映射,由於要求一個元素只能對一種元素 public static boolean isIsomorphic(String s, String t) { Map<Character,Character> map = new HashMap<>(); Map<Character,Character> map2 = new HashMap<>(); if(s.length() != t.length()){ return false; } for(int i = 0;i< s.length();i++){ Character c = s.charAt(i); Character c2 = t.charAt(i); if(map.containsKey(c)){ if(!c2.equals(map.get(c))){ return false; } }else{ map.put(c,c2); } } for(int i = 0;i< t.length();i++){ Character c = t.charAt(i); Character c2 = s.charAt(i); if(map2.containsKey(c)){ if(!c2.equals(map2.get(c))){ return false; } }else{ map2.put(c,c2); } } return true; }
//在相應位置維護一個值 public static boolean isIsomorphic2(String s1, String s2) { int[] m = new int[512]; for (int i = 0; i < s1.length(); i++) { if (m[s1.charAt(i)] != m[s2.charAt(i)+256]) { return false; } m[s1.charAt(i)] = m[s2.charAt(i)+256] = i+1; } return true; }
//利用了 map的put特性 @return previous value, or null if none public static boolean isIsomorphic3(String s, String t) { Map m = new HashMap(); for (Integer i=0; i<s.length(); ++i) { if (m.put(s.charAt(i), i) != m.put(t.charAt(i)+"", i)) { return false; } } return true; }
git:https://github.com/woshiyexinjie/leetcode-xin/tree/master/src/main/java/com/helloxin/leetcode/algorithms string