Java Solution beats 97.08% in 3ms


  • 1
    E
    public boolean isAnagram(String s, String t) {
        if(s == null || t == null)
            return false;
        if(s.length() != t.length())
            return false;
        char[] ss = s.toCharArray();
        char[] ts = t.toCharArray();
        //one 'char' type takes 8bits. 256 char values at most.
        int[] srecord = new int[256];
        int[] trecord = new int[256];
        int len = s.length();
        for(int i = 0; i < len; ++i){
            srecord[ss[i]] ++;
            trecord[ts[i]] ++;
        }
        for(int i = 0; i <  256; ++i){
            if(srecord[i] != trecord[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.