Skip to content

389. Find the Difference

Easy

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.

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        fs = self.freq(s)
        ft = self.freq(t)

        for k, v in ft.items():
            if k not in fs or fs[k] < v:
                return k

    def freq(self, s):
        d = {}
        for c in s:
            d[c] = d.get(c, 0) + 1
        return d