aboutsummaryrefslogtreecommitdiff
path: root/find_difference.py
blob: f0b26a6246fd4b51e8d6ef1820c15444d42965c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 389. Find the Difference

"""
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(object):
    def findTheDifference(self, s, t):
        ans = 0
        for i in s:
            ans ^= ord(i)
        for i in t:
            ans ^= ord(i)
        return chr(ans)


if __name__ == "__main__":
    obj = Solution()
    print(obj.findTheDifference("abcd", "abcde"))  # expect: e
    print(obj.findTheDifference("", "y"))  # expect: y