[
string
hash table
counter
bit manipulation
]
Leetcode 0389 Find the Difference
Problem statement
https://leetcode.com/problems/find-the-difference/
Solution
One solution is just to use counters and then find the difference, time complexity is O(n)
, space is (26)
number of letters.
Complexity
Code
class Solution:
def findTheDifference(self, s, t):
return list((Counter(t) - Counter(s)).keys())[0]
Remark
There is another O(1)
space solution, using bit manipulations: just evaluate bit-wise xor for all symbols in two words and you answer will be exactly symbol we are looking for.