Simple and short Java solution


  • 0
    N
    public class Solution {
      	public static boolean isIsomorphic(String s, String t) {
    
    		int num=1,num2=1;
    
    		int[] ch1 = new int[95];
    		int[] ch2 = new int[95];
    
    		for (int i = 0; i < s.length(); i++) {
    			if (ch1[s.charAt(i) - ' '] == 0) {
    				ch1[s.charAt(i) - ' '] = num;
    				num++;
    			} 
    			if (ch2[t.charAt(i) - ' '] == 0) {
    				ch2[t.charAt(i) - ' '] = num2;
    				num2++;
    			} 
    			if(ch2[t.charAt(i) - ' ']!=	ch1[s.charAt(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.