288ms fastest Java solution so far


  • 0
    M

    288ms Java solution. OJ told me this is fastest so far (at least one of the fastest). Please do point out where improvement can be made.

    public boolean isIsomorphic(String s, String t) {
    		if (s.length() == 0) return true;
    		
    		char[] stable = new char[128];
    		char[] ttable = new char[128];
    		for (int i = 0; i < 128; ++i){
    			stable[i] = 0;
    			ttable[i] = 0;
    		}
    
    		char[] sc = s.toCharArray();
    		char[] tc = t.toCharArray();
    		
    		for (int i = 0; i < s.length(); ++i){
    			char schar = sc[i];
    			char tchar = tc[i];
    			if (stable[schar] == 0 && ttable[tchar] == 0){
    				stable[schar] = tchar;
    				ttable[tchar] = schar;
    			}else if(stable[schar] == tchar && ttable[tchar] == schar){
    				continue;
    			}else{
    				return false;
    			}
    		}
    		
    		return true; 
    		
        }

Log in to reply
 

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