[220207] Find the Difference

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, t):

        c = 0

        for char in t:
            # 使用 ASCII 码记录
            c ^= ord(char)
        
        # ^ 计算,抵消相同数字 (x ^ x == 0)
        for char in s:
            c ^= ord(char)

        return chr(c)
from collections import Counter


class Solution:
    def findTheDifference(self, s, t):

        c_s = Counter(s)
        c_t = Counter(t)

        for item in c_t:
            # 分别比较每个字母出现的次数
            if not c_t[item] == c_s[item]:
                return item

上一篇:Python06-2容器


下一篇:CF347A Difference Row 题解