One pass via bit manipulation without HashMap


  • 1
    F
        public char findTheDifference(String s, String t) {
            char result = 0;
            for (char c : s.toCharArray()) {
                result ^= c;
            }
            for (char c : t.toCharArray()) {
                result ^= c;
            }
            return result;
        }

  • 0
    class Solution {
    public:
        char findTheDifference(string s, string t) {
            char c = t.back();
            for (int i = 0; i < s.size(); i++)
                c ^= s[i] ^ t[i];
            return c;
        }
    };
    

Log in to reply
 

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