Find the Difference - Problem
You are given two strings s and t.
String t is generated by randomly shuffling string s and then adding one more letter at a random position.
Return the letter that was added to t.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abcd", t = "abcde"
›
Output:
"e"
💡 Note:
The letter 'e' was added to the end of string s to create string t.
Example 2 — Added in Middle
$
Input:
s = "abc", t = "abcd"
›
Output:
"d"
💡 Note:
The letter 'd' was added to string s. Even though t is shuffled, we can detect the extra character.
Example 3 — Single Character
$
Input:
s = "", t = "y"
›
Output:
"y"
💡 Note:
Edge case: empty string s, and t contains only the added character 'y'.
Constraints
- 0 ≤ s.length ≤ 1000
- t.length == s.length + 1
- s and t consist of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Original
Start with string s
2
Shuffle + Add
Shuffle s and add one character
3
Find Extra
Identify the added character
Key Takeaway
🎯 Key Insight: XOR cancels identical characters, leaving only the unique added character
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code