Java 7ms simple solution,beats 95%


  • 1
    F
    public  boolean isIsomorphic(String s, String t) {
    		if(s == null && t == null){
    			return true;
    		}
    		if(s == null || t == null){
    			return false;
    		}
    		if(s.length() != t.length()){
    			return false;
    		}
    		
    		int[] sMap = new int[256];
    		int[] tMap = new int[256];
    		
    		int[] cMap = new int[s.length()];
    		int index = 0;
    		
    		for(int i = 0;i<s.length();i++){
    			char c = s.charAt(i);
    			int j =0;
    			if(sMap[c]>0){
    				j = sMap[c];
    			}else{
    				j = ++index;
    				sMap[c] = j;
    			}
    			cMap[i] = j;
    			
    		}
    		
    		index = 0;
    		
    		for(int i=0;i<t.length();i++){
    			char c = t.charAt(i);
    			int j = 0;
    			if(tMap[c]>0){
    				j = tMap[c];
    			}else{
    				j = ++index;
    				tMap[c]= j;
    			}
    			
    			if(j != cMap[i]){
    				return false;
    			}
    			
    		}
    		
    		return true;
    
    	}
    

Log in to reply
 

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