327. Find the Difference

Easy
Frequency Difference
Hashing

Problem

You are given two strings s and t. String t is created by randomly shuffling string s and then adding one extra character. Return the character that was added.

Examples

Example 1

Input: "abcd", "abcde"
Output: "e"

Example 2

Input: "", "y"
Output: "y"

Example 3

Input: "a", "aa"
Output: "a"
Constraints

0 <= s.length <= 1000

t.length == s.length + 1

s and t consist of lowercase English letters.

Hints

?? Count characters in one string and subtract counts using the other.

?? Because exactly one extra character exists, XOR is also possible.

Expected Complexity
Time: O(n)
Space: O(1) or O(alphabet size)
Follow-up

Can you solve it in O(1) extra space using XOR?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
"abcd", "abcde"
Expected
"e"