5ms Java Solution


  • 4
    J

    Here's my 5ms solution. Did a few things like change to char array for speed.

    public class Solution {
    public boolean isIsomorphic(String s, String t) {
    if(s.length() != t.length())
    return false;

        int counter = 1;
        int poS, poT;
        char[] sa = s.toCharArray();
        char[] ta = t.toCharArray();
    
        int[] arrS = new int[256];
        int[] arrT = new int[256];
    
        for(int i = 0; i != s.length(); i++){
            poS = sa[i];
            poT = ta[i];
            if(arrS[poS] != arrT[poT]) {
                return false;
            } else if(arrS[poS] == 0) {
                arrS[poS] = arrT[poT] = counter++;
            } 
        }
        return true;   
    }
    

    }


Log in to reply
 

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