389. Find the Difference
Find the Difference (LeetCode 389) 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.
How to solve Find the Difference
Find the Difference is a XOR for Single and Missing Numbers problem, part of Bit Manipulation Patterns. Use XOR properties to find single number, missing number, and duplicates.
Work towards the solution in these steps:
- Think about how you can compare the characters of both strings to find the extra letter.
- Consider using a data structure like a hash map or an array to count the frequency of each character in both strings.
- Is there a way to leverage a mathematical or bitwise operation to identify the extra character without explicit counting?
The reference solution runs in O(n) time and O(1) space.
The Find the Difference solution and editorial walks through the full approach with reference code.
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
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
- Sum of Two Integers — Medium, same subpattern
- Bitwise AND of Numbers Range — Medium, same pattern
- Counting Bits — Easy, same pattern
Find the Difference FAQ
What is the LeetCode number of Find the Difference?
Find the Difference is LeetCode problem 389, rated Easy.
What pattern is used to solve Find the Difference?
Find the Difference is a XOR for Single and Missing Numbers problem. It belongs to the Bit Manipulation Patterns pattern, in the Bitwise XOR - Finding Single/Missing Number subpattern.
What is the time complexity of Find the Difference?
The reference solution runs in O(n) time and O(1) space.
Which companies ask Find the Difference?
Find the Difference has been reported in interviews at Google, Amazon.