Java 8ms (O(n))


  • 2
    L
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            if(s.length() != t.length()) return false;
    
            int N = s.length();
            int[] mst = new int[256];
            int[] mts = new int[256];
            
            for(int i = 0; i < N; i++) {
                char cs = s.charAt(i);
                char ct = t.charAt(i);
                
                if((mst[cs] == 0) && (mts[ct] == 0)) {
                    mst[cs] = ct;
                    mts[ct] = cs;
                }
                
                if((mst[cs] != ct) || (mts[ct] != cs)) 
                    return false;
            }
            
            return true;
        }
    }

Log in to reply
 

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