str_diff_solution (Python) 存在问题

作者: withjak创建于 2021年4月26日更新于 2023年3月16日
标签needs-review

class Solution(object):

def find_diff(self, str1, str2):
    if str1 is None or str2 is None:
        raise TypeError('str1 or str2 cannot be None')
    seen = {}
    for char in str1:
        if char in seen:
            seen[char] += 1
        else:
            seen[char] = 1
    for char in str2:
        try:
            seen[char] -= 1
        except KeyError:
            return char
        if seen[char] < 0:
            return char
    
    for char, count in seen.items():
        return char

`

内容来源: donnemartin/interactive-coding-challenges