其他分享
首页 > 其他分享> > [220207] Find the Difference

[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

标签:random,return,self,Counter,char,item,220207,Difference,Find
来源: https://blog.csdn.net/yzh3558/article/details/122808022