Java solution using toarray and sort


  • 1
        if (s == null || t == null || s.length() != t.length()) {
            if (s == null && t == null) {
                return true;
            }
            return false;
        }
    
        char[] sc = s.toCharArray();
        char[] tc = t.toCharArray();
        
        Arrays.sort(sc);
        Arrays.sort(tc);
        
        for (int i = 0; i < sc.length; i++) {
            if (sc[i] != tc[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.