package com.fishercoder.solutions;

import java.util.HashMap;
import java.util.Map;
/**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.

 For example,
 Given "egg", "add", return true.

 Given "foo", "bar", return false.

 Given "paper", "title", return true.

 Note:
 You may assume both s and t have the same length.*/
public class _205 {
    /**space should be O(1) since it only has alphabetic letters which are capped at 52.*/

    public boolean isIsomorphic(String s, String t) {
        if (s == null || s.length() == 0) {
            return (t == null || t.length() == 0);
        }
        if (t == null || t.length() == 0) {
            return (s == null || s.length() == 0);
        }
        char[] schar = s.toCharArray();
        char[] tchar = t.toCharArray();
        Map<Character, Character> map = new HashMap();
        if (s.length() != t.length()) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(schar[i])) {
                if (map.get(schar[i]) != tchar[i]) {
                    return false;
                }
            } else {
                if (map.containsValue(tchar[i])) {
                    return false;//this line is necessary for this case: ("ab", "aa")
                }
                map.put(schar[i], tchar[i]);
            }
        }
        return true;
    }

}

@@ -80,16 +80,50 @@
 Map;%0A/**
+%0A * 205. Isomorphic Strings%0A *%0A * 
 Given tw
@@ -748,16 +748,56 @@
  52.*/%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -846,24 +846,28 @@
 ) %7B%0A        
+    
 if (s == nul
@@ -882,32 +882,36 @@
 ength() == 0) %7B%0A
+    
             retu
@@ -945,34 +945,42 @@
  == 0);%0A        
-%7D%0A
+    %7D%0A    
         if (t ==
@@ -1011,32 +1011,36 @@
 ) %7B%0A            
+ 
+   
 return (s == nul
@@ -1070,26 +1070,32 @@
         
-%7D%0A
+    %7D%0A  
         
 char%5B%5D s
@@ -1082,24 +1082,26 @@
 %7D%0A          
+  
 char%5B%5D schar
@@ -1124,24 +1124,28 @@
 ();%0A        
+   
+ 
 char%5B%5D tchar
@@ -1172,16 +1172,20 @@
         
+   
+ 
 Map%3CChar
@@ -1219,24 +1219,28 @@
  HashMap();%0A
+    
         if (
@@ -1271,32 +1271,36 @@
 ) %7B%0A            
+    
 return false;%0A  
@@ -1297,34 +1297,42 @@
  false;%0A        
+ 
-%7D%0A
+   %7D%0A    
         for (int
@@ -1366,32 +1366,36 @@
 ) %7B%0A            
+  
+  
 if (map.contains
@@ -1403,32 +1403,36 @@
 ey(schar%5Bi%5D)) %7B%0A
+    
                 
@@ -1480,32 +1480,36 @@
                 
+   
+ 
 return false;%0A  
@@ -1514,34 +1514,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D el
@@ -1561,32 +1561,36 @@
                 
+    
 if (map.contains
@@ -1600,32 +1600,36 @@
 ue(tchar%5Bi%5D)) %7B%0A
+    
                 
@@ -1706,34 +1706,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -1773,34 +1773,42 @@
 %5D);%0A            
+   
+ 
 %7D%0A
+    
         %7D%0A      
@@ -1793,32 +1793,36 @@
 %7D%0A            %7D%0A
+    
         return t
@@ -1830,13 +1830,22 @@
 ue;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 205