Java 3ms beats 99.54% by using boolean


  • 0
    S

    First, I use toCharArray()
    Second, I create two arrays dict and record

    dict is used to save characters' relation, record is used to record whether t's character has been save.

    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            char[] dict = new char[128];
            boolean[] record = new boolean[128];
            char[] ss = s.toCharArray();
            char[] ts = t.toCharArray();
            char cS;
            char cT;
            for(int i = 0; i < ss.length; i++){
                cS = ss[i];
                cT = ts[i];
                if(dict[cS] == 0 && record[cT] == false) {
                    dict[cS] = cT;
                    record[cT] = true;
                }
                else if(dict[cS] != cT) return false;
            }
            return true;
        }
    }
    

Log in to reply
 

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