LeetCode 389 Find the Difference (Python)

Posted by 小明MaxMing on September 24, 2020

题目

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

解题思路

使用字典统计两个字符串出现字符次数的差

代码

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        dic = defaultdict(int)
        for c in s:
            dic[c] += 1
        for c in t:
            dic[c] -= 1
        for k in dic:
            if dic[k] != 0:
                return k

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅