389. Find the Difference - Explanation
Description
You are given two strings s and t.
String t is generated by random shuffling string s and then add one more letter at a random position.
Return the letter that was added to t.
Example 1:
Input: s = "abcd", t = "abcde"
Output: "e"Explanation: 'e' is the letter that was added.
Example 2:
Input: s = "", t = "y"
Output: "y"Constraints:
0 <= s.length <= 1000t.length == s.length + 1sandtconsist of lowercase English letters.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Hash Maps / Frequency Counting - Used to count character occurrences and find the extra character
- ASCII Values - Understanding character-to-integer conversion for sum-based solutions
- Bit Manipulation (XOR) - The optimal solution uses XOR properties where a ^ a = 0 to find the unique character
1. Two Hash Maps
Intuition
String t is formed by shuffling string s and adding one extra character. By counting the frequency of each character in both strings, the added character will have a higher count in t than in s. We can compare these counts to find the difference.
Algorithm
- Create two count arrays of size 26 for characters in
sandt. - Count the frequency of each character in both strings.
- Compare the counts. The character where
countT[i] > countS[i]is the answer. - Return that character.
Time & Space Complexity
- Time complexity:
- Space complexity: since we have at most different characters.
2. One Hash Map
Intuition
Instead of using two separate count arrays, we can use a single array. First count all characters from t, then subtract counts for characters in s. The character left with count 1 is the added character.
Algorithm
- Create a count array of size 26.
- Increment counts for each character in
t. - Decrement counts for each character in
s. - Find and return the character with count equal to
1.
Time & Space Complexity
- Time complexity:
- Space complexity: since we have at most different characters.
3. Sorting
Intuition
After sorting both strings, corresponding characters at each position should match since t is a shuffled version of s plus one character. The first position where they differ reveals the added character. If all positions match, the added character is at the end of t.
Algorithm
- Sort both strings
sandt. - Compare characters at each position.
- Return the first character in
tthat differs from the corresponding character ins. - If all characters match, return the last character of
t.
Time & Space Complexity
- Time complexity:
- Space complexity: or depending on the sorting algorithm.
4. Difference Between ASCII Values
Intuition
Each character has an ASCII value. If we sum the ASCII values of all characters in t and subtract the sum of ASCII values in s, the result equals the ASCII value of the added character. This works because all matching characters cancel out.
Algorithm
- Compute the sum of ASCII values for all characters in
s. - Compute the sum of ASCII values for all characters in
t. - Subtract
sumSfromsumT. - Convert the result to a character and return it.
Time & Space Complexity
- Time complexity:
- Space complexity: extra space.
5. Difference Between ASCII Values (Optimal)
Intuition
We can optimize space by using a single variable instead of two separate sums. By subtracting ASCII values from s and adding ASCII values from t to the same variable, we compute the difference in one pass through both strings.
Algorithm
- Initialize
res = 0. - For each character in
s, subtract its ASCII value fromres. - For each character in
t, add its ASCII value tores. - Convert
resto a character and return it.
Time & Space Complexity
- Time complexity:
- Space complexity: extra space.
6. Bitwise XOR
Intuition
XOR has a useful property: a ^ a = 0 and a ^ 0 = a. If we XOR all characters from both strings together, each character that appears in both s and t will cancel out (XOR with itself gives 0). The only character remaining is the added one, since it appears an odd number of times total.
Algorithm
- Initialize
res = 0. - XOR
reswith each character's ASCII value ins. - XOR
reswith each character's ASCII value int. - Convert
resto a character and return it.
Time & Space Complexity
- Time complexity:
- Space complexity: extra space.
Common Pitfalls
Forgetting to Handle Empty String s
When s is an empty string, t contains exactly one character (the added one). Solutions that iterate through s first or rely on finding a mismatch between sorted strings must handle this edge case. The XOR and sum-based approaches naturally handle this since XORing or summing over an empty string contributes nothing.
Comparing Characters Instead of Counts
A common mistake is to iterate through both strings and return the first character in t that does not appear in s. This fails when the added character is a duplicate of an existing character. For example, if s = "a" and t = "aa", the added character is 'a', but a naive check would miss it since 'a' already exists in s.
Sign in to join the discussion