-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
37 lines (33 loc) · 1.05 KB
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
# Bit Manipulation Solution
#-------------------------------------------------------------------------------
class Solution:
def findTheDifference(self, s, t):
ans = 0
for c in s + t:
ans ^= ord(c)
return chr(ans)
#-------------------------------------------------------------------------------
# Dict Solution
#-------------------------------------------------------------------------------
class Solution:
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
chars = {}
for c in s:
if c in chars:
chars[c] += 1
else:
chars[c] = 1
for c in t:
if c in chars and chars[c] > 0:
chars[c] -= 1
else:
return c
return None
#-------------------------------------------------------------------------------