Java solution 28ms for 30 tests


  • 1
    V
    	public boolean isIsomorphic(String s, String t) {
            int n1 = s.length();
            if( n1 != t.length()) return false;
            Hashtable<Character, Character> hash = new Hashtable<Character, Character>();
            for( int i = 0; i < n1; i++ ) {
                if( hash.containsKey(s.charAt(i)) )  {
                	if( ( hash.get(s.charAt(i)) != t.charAt(i) ) )
                         return false;
                }
                else {
                    if ( hash.containsValue(t.charAt(i)) )
                        return false;
                    hash.put(s.charAt(i), t.charAt(i));
                }
            }
            return true;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.