python using mapping


  • 0
    X
    class Solution(object):
        def findTheDifference(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: str
            """
            l = len(s)
            a = []
            c = 0
            for i in range(l+1):
                if t[i] not in s:
                    a.append(t[i])
                else:
                    c += 1
            if c == l+1:
                c1 = dict()
                c2 = dict()
                for s1 in s:
                    c1[s1] = c1.get(s1,0)+1
                for s2 in t:
                    c2[s2] = c2.get(s2,0)+1
                lst1 = list()
                for key,val in c1.items():
                    lst1.append((key,val))
                lst2 = list()
                for key,val in c2.items():
                    lst2.append((key,val))
                lst1.sort()
                lst2.sort()
                for j in range(len(lst1)):
                    if lst1[j][1] != lst2[j][1]:
                        a.append(lst2[j][0])
            return(a[0])
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.