Find the Difference: XOR to Spot the Extra Character
Find the Difference is LeetCode 389, rated Easy. You are given two strings s and t. String t is generated by randomly shuffling s and then adding one extra character at a random position. Your job is to find that added character.
For example, if s = "abcd" and t = "abcde", the answer is 'e' because it is the only character in t that was not in s.
You could count character frequencies with a hash map, but there is a cleaner way. XOR gives you the answer in one pass with no extra data structures.
The key insight
XOR has a cancellation property: when you XOR a value with itself, the result is 0. And XOR-ing anything with 0 gives back the original value. If you XOR every character from both s and t together, every character that appears in both strings cancels out. The only character left is the one that was added.
This works because t contains exactly the same characters as s plus one extra. Every character from s has a matching partner in t, and those pairs cancel to 0. The extra character has no partner, so it survives.
The approach
- Initialize
result = 0 - XOR every character in
sintoresult - XOR every character in
tintoresult - Convert
resultback to a character
After step 3, every shared character has been XOR-ed exactly twice (once from s, once from t), cancelling to 0. The added character has been XOR-ed exactly once, so it is all that remains.
The code
def find_the_difference(s: str, t: str) -> str:
result = 0
for ch in s:
result ^= ord(ch)
for ch in t:
result ^= ord(ch)
return chr(result)
Step 1: Initialize result = 0
Start with a result of 0. XOR with 0 preserves any value, so this is the neutral starting point.
result = 0Step 2: XOR every character in s
Walk through s = "abcd" and XOR each character's ordinal value into result. After this loop, result holds a ^ b ^ c ^ d.
result = 0 ^ 97 ^ 98 ^ 99 ^ 100 (= 96 in decimal)Step 3: XOR every character in t
Now walk through t = "abcde" and XOR each character into result. The a, b, c, d characters appear in both strings, so they cancel out pairwise.
result = 96 ^ 97 ^ 98 ^ 99 ^ 100 ^ 101Step 4: Pairs cancel, extra survives
Each character shared between s and t has been XOR-ed twice, which cancels to 0. Only the extra character 'e' (ordinal 101) survives.
result = 101 → chr(101) = 'e'Complexity
| Time | Space | |
|---|---|---|
| Total | O(n) | O(1) |
Time: O(n). You iterate through both strings once. String s has length n and t has length n + 1, so the total work is 2n + 1 operations, which is O(n).
Space: O(1). You only use a single integer variable result. No hash maps, no arrays, no extra storage that grows with input size.
Building blocks
XOR cancellation
XOR cancellation is the same technique used in Single Number. The core idea: when you XOR a collection of values and every value appears an even number of times except one, that one value is the result. Here, each shared character appears twice (once in s, once in t), so it cancels. The added character appears once, so it survives.
This pattern shows up whenever you need to find an unpaired element. Recognize the signal: two collections that are almost identical, differing by exactly one element.
Edge cases
- Single character. If
sis empty andthas one character, the loop oversdoes nothing and the loop overtXORs that single character with 0. The result is the added character itself.0 ^ x = xhandles this cleanly. - Added character is the same as an existing one. If
s = "aa"andt = "aaa", the threeavalues XOR together:a ^ a ^ a ^ a ^ a = a. Two fromsand three fromtgive five total XOR operations. Four cancel in pairs, leaving onea. The algorithm does not care whether the added character is new or a duplicate.
From understanding to recall
You have seen XOR cancellation before if you solved Single Number or Missing Number. The pattern is always the same: initialize to 0, XOR everything in, and the unpaired value falls out. What changes between problems is how you frame the two "collections" that overlap.
For Find the Difference, the two collections are the characters of s and the characters of t. For Single Number, it is the array elements paired against themselves. For Missing Number, it is array values paired against their indices. The underlying mechanic is identical.
Drill the XOR accumulation loop a few times from scratch. Once the pattern is automatic, you will recognize it instantly whenever a problem asks you to find the one element that does not have a match.
Related posts
- Single Number - XOR to find the lone element
- Missing Number - XOR or sum to find what is absent
- Single Number III - XOR extended to two unique elements