class Solution:
def findTheDifference(self, s: str, t: str) -> str:
# Using XOR to find the extra character
result = 0
for ch in s:
result ^= ord(ch)
for ch in t:
result ^= ord(ch)
return chr(result)
class Solution {
public:
char findTheDifference(string s, string t) {
int result = 0;
for (char ch : s) result ^= ch;
for (char ch : t) result ^= ch;
return result;
}
};
class Solution {
public char findTheDifference(String s, String t) {
int result = 0;
for (char ch : s.toCharArray()) result ^= ch;
for (char ch : t.toCharArray()) result ^= ch;
return (char)result;
}
}
var findTheDifference = function(s, t) {
let result = 0;
for (let ch of s) {
result ^= ch.charCodeAt(0);
}
for (let ch of t) {
result ^= ch.charCodeAt(0);
}
return String.fromCharCode(result);
};
You are given two strings, s
and t
. String t
is generated by shuffling string s
and then adding one extra character at a random position. Your task is to find and return the extra character that was added to t
.
s
and t
consist of lowercase English letters.t
that is not in s
.s
can only match one character in t
.
At first glance, the problem seems to require comparing each character in t
with those in s
and finding which one is extra. A brute-force approach would involve checking each character of t
against s
and removing matches, but that would be inefficient for longer strings.
To optimize, we recognize that since t
is just s
with one extra character, their combined character counts will differ by exactly one for the added character. This insight suggests using counting techniques or mathematical operations to efficiently spot the difference.
Another clever approach is to use the XOR operation, since XOR-ing two identical numbers cancels them out (resulting in zero), and XOR is commutative and associative. If we XOR all characters in both strings, all matching pairs will cancel out, leaving only the extra character from t
.
result
) to zero.s
and XOR its ASCII (or Unicode) value with result
.t
.This approach is both time and space efficient. We use a variable to accumulate the XOR, and the runtime is linear in the length of the strings.
Why XOR? Since a ^ a = 0
and 0 ^ b = b
, all matching characters in s
and t
will cancel out, and only the extra character remains.
Let's walk through an example with s = "abcd"
and t = "abcde"
.
result = 0
.s
:
result ^= ord('a')
→ result becomes 97result ^= ord('b')
→ result becomes 3result ^= ord('c')
→ result becomes 96result ^= ord('d')
→ result becomes 4t
:
result ^= ord('a')
→ result becomes 101result ^= ord('b')
→ result becomes 7result ^= ord('c')
→ result becomes 100result ^= ord('d')
→ result becomes 0result ^= ord('e')
→ result becomes 101'e'
.
Thus, the function returns 'e'
, which is the extra character in t
.
t
, check if it exists in s
and remove it if found. This results in O(n^2) time complexity, where n is the length of the strings, due to repeated searches and removals.
The XOR method is optimal in both time and space.
To solve the "Find the Difference" problem, we leverage the properties of the XOR operation to efficiently detect the extra character added to string t
. This method is both simple and elegant: by XOR-ing all characters in both strings, all matching pairs cancel out, leaving only the extra character. This approach is optimal, with linear time and constant space complexity, and is easy to implement across multiple programming languages.