Problem
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.
Algorithm
Use a list to count the letters in “s”. Then remove the letter in “t” from the list. If the letter only appear in “t” return it.
Code
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
sCnts = [0] * 26
for c in s:
sCnts[ord(c) - ord('a')] += 1
for c in t:
if sCnts[ord(c) - ord('a')] > 0:
sCnts[ord(c) - ord('a')] -= 1
else:
return c
return ""