Concise Java solution using XOR


  • 0

    Use XOR to find the difference between s and t.

    public class Solution {
        public char findTheDifference(String s, String t) {
            char c = 0;
            for (int i = 0; i < s.length(); i++) {
                c ^= s.charAt(i) ^ t.charAt(i);
            }
            return (char) (c ^ t.charAt(t.length() - 1));
        }
    }
    

Log in to reply
 

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