Simple Java Solution using XOR


  • 0
    X

    First convert the 2 string to 1 single char array. Since char is essentially the int, this problem can be rephrased to: in an int array, every number appears twice except one, find that guy. So the code could be like this:

    public char findTheDifference(String s, String t) {
            char[] stArray = (s + t).toCharArray();
            char res = 0;
            for (char elem : stArray) {
                res ^= elem;
            }
            
            return res;
    }
    

Log in to reply
 

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