Did I miss some basic `char` arithmetics?


  • 0
    2

    it looks like in the inner if loop, even when the two characters are different, the character isn't being added to the char

    i ended up using a char[] instead and it was accepted(not a very efficient solution, only beating 40% of submissions), but still bugged by this..

    public class Solution {
        public char findTheDifference(String s, String t) {
            char[] sArr = s.toCharArray();
            char[] tArr = t.toCharArray();
            
            Arrays.sort(sArr);
            Arrays.sort(tArr);
            
            
            char ans = ' ';
            
            for(int i=0;i<tArr.length-1;i++){
                if(tArr[i] != sArr[i]){
                    ans+t=Arr[i];
                    return ans;
                }
            }
            
            return tArr[tArr.length-1];
        }
    }
    

Log in to reply
 

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