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
Find the Difference: s = "abc", t = "abcd""abc"String s"abcd"String t"d"Added charshuffle + adddetectGoal: Find which character was added to transform s into t🎯 Key Insight: Use XOR or frequency counting to find the difference
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
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
185.0K Views
Medium Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen