Saturday, November 4, 2017

389. Find the Difference

class Solution:
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        def construct_dict(a_string):
            a_dict = {}
            for i in range(len(a_string)):
                if a_string[i] in a_dict:
                    a_dict[a_string[i]] += 1
                else:
                    a_dict[a_string[i]] = 1
            return a_dict
        dict_1 = construct_dict(s)
        dict_2 = construct_dict(t)
        for a_key in dict_2.keys():
            try:
                dict_1[a_key]
            except KeyError:
                return a_key
            else:
                if dict_1 [a_key] != dict_2 [a_key]:
                    return a_key  

No comments:

Post a Comment