Java solution 10ms with sort


  • 0
    D
        public char findTheDifference(String s, String t) {
            char[] sArray = s.toCharArray();
            char[] tArray = t.toCharArray();
            Arrays.sort(sArray);
            Arrays.sort(tArray);
            for(int i =0; i<sArray.length-1;i++){
                if(sArray[i]!=tArray[i]){
                    return tArray[i];
                }
            }
            return tArray[tArray.length-1];
        }
    
    

Log in to reply
 

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