↓ 跳过正文
  1. 科学/
  2. 计算机/
  3. 算法/
  4. Leetcode/
  5. 0300-0399/

0389 找不同

Solution

from collections import Counter

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        cnt = Counter(s)
        for c in t:
            cnt[c] -= 1
            if cnt[c] < 0:
                return c

# 本地测试
if __name__ == "__main__":
    sol = Solution()
    print(sol.findTheDifference("abcd", "abcde")) # 'e'
    print(sol.findTheDifference("", "y"))          # 'y'

Solution 2

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        a = sum(ord(c) for c in s)
        b = sum(ord(c) for c in t)
        return chr(b - a)

# 本地测试
if __name__ == "__main__":
    sol = Solution()
    print(sol.findTheDifference("abcd", "abcde")) # e
    print(sol.findTheDifference("", "y")) # y