Find the Difference
Find the one letter added to a shuffled copy of a string, by letting matching letters cancel out with XOR.
Given two strings stts
Example 1
Output: 'e'
Example 2
Output: 'y'
Example 3
Output: 'b'
The Idea
Picture four letter tiles in a bag: a, b, c and d. A friend copies them, slips in one more tile, shakes the bag and hands it back. In Example 1 the new bag holds a, b, c, d and e, and with so few tiles you see at once that e is the newcomer.
Example 3 is trickier. The string s"ab"t"bab"tstsst
Now put the letters of both strings in one row: a, b, then b, a, b. Each letter of st
It can, because a Java char11000011100010^
So 97 ^ 980000011
Look at the picture below. Each row takes one letter, shown in the column labelled "the letter in binary", and XORs it into the column labelled "running XOR".
The row labelled 'a' from s starts the running XOR at 'a' itself. After 'b' from s it reads 00000111100001
The last row XORs the final 'b' into those zeros, and the running XOR ends at 1100010
Solution Approach
Turning that idea into code means weighing it against the simpler ways of sorting or counting, then settling how to keep the running XOR and how to turn it back into a letter. Adding up the codes is a close alternative to XOR.
Sorting or counting the letters
What would a first attempt look like? One way is to sort both strings and walk along them side by side until they differ. For "ab""bab""ab""abb"t
Another way is to count each letter of tsst{a, b}
Keeping the running XOR
Where does the running XOR live? In one intxintcharx
Since order does not matter, the program can XOR all of st
Turning the result back into a letter
After both loops, what is in x(char) x
Adding the codes instead
Is XOR the one trick that works? Adding the codes of tschar
Java Solution
The method findTheDifference
- First, set an
namedint to 0. It holds the running XOR.x - Loop over
with an indexs , and XOR each characteri intos.charAt(i) .x - A second loop does the same for every character of
. Each letter oft with a twin int cancels that twin out ofs .x - After the second loop,
holds the code of the added letter, so returnx .(char) x
The mainrun"aab""hello"
public class FindTheDifference {
public static char findTheDifference(String s, String t) {
int x = 0;
for (int i = 0; i < s.length(); i++) {
x ^= s.charAt(i);
}
for (int i = 0; i < t.length(); i++) {
x ^= t.charAt(i); // a letter that appears in both strings cancels out
}
return (char) x; // only the added letter is left
}
static void run(String s, String t) {
System.out.println("s = \"" + s + "\", t = \"" + t + "\" -> '"
+ findTheDifference(s, t) + "'");
}
public static void main(String[] args) {
run("abcd", "abcde");
run("", "y");
run("ab", "bab");
run("aab", "abaa");
run("hello", "ohlelz");
}
}
Running it prints:
s = "abcd", t = "abcde" -> 'e'
s = "", t = "y" -> 'y'
s = "ab", t = "bab" -> 'b'
s = "aab", t = "abaa" -> 'a'
s = "hello", t = "ohlelz" -> 'z'
Look at the line for "ab""bab"sx
The line for "aab""abaa""hello""ohlelz"
Complexity
The method reads each character of both strings once and keeps a single number.
- Time: O(n), with n the length of
, because each of its n letters and each of the n - 1 letters oft is XORed intos once.x - Space: O(1), because
is onex however long the strings are, andint reads the strings without copying them.charAt
The same cancelling idea solves