Find the Difference
Find the Difference is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Bit Manipulation Patterns pattern, in the Bitwise XOR - Finding Single/Missing Number subpattern.
Problem statement
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 <= 1000
- t.length == s.length + 1
- s and t consist of lowercase English letters.
Topics and companies
Topics: Hash Table, String, Bit Manipulation, Sorting.
Reported in interviews at Google, Amazon.
How to practise Find the Difference on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Find the Difference in the code editor, or read the Find the Difference editorial for a worked solution with its approach and complexity analysis.
Where Find the Difference sits in the DSA pattern sheet
- Bit Manipulation Patterns pattern guide
- Bitwise XOR - Finding Single/Missing Number subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Find the Difference
Other problems that use the same Bitwise XOR - Finding Single/Missing Number and Bit Manipulation Patterns techniques:
- Missing Number — Easy, same subpattern
- Single Number — Easy, same subpattern
- Single Number II — Medium, same subpattern
- Counting Bits — Easy, same pattern
- Number of 1 Bits — Easy, same pattern
- Power of Four — Easy, same pattern
Preparing your workspace
Progress is saved as you study.