# 1-liners and 2-liner in Python

• Using XOR:

``````class Solution(object):
def findTheDifference(self, s, t):
return chr(reduce(operator.xor, map(ord, s + t)))
``````

Using `collections.Counter()`:

``````class Solution(object):
def findTheDifference(self, s, t):
return list((collections.Counter(t) - collections.Counter(s)))[0]
``````

A 2-liner here using `sorted()`:

``````class Solution(object):
def findTheDifference(self, s, t):
s, t = sorted(s), sorted(t)
return t[-1] if s == t[:-1] else [x[1] for x in zip(s, t) if x[0] != x[1]][0]
``````

• A few one-liners using `sorted()`:

`return next((c for c, d in zip(sorted(t), sorted(s)) if c != d), max(t))`
`return next(c for c, d in zip(sorted(t), sorted(s) + [None]) if c != d)`
`return next(c for c, d in map(None, sorted(t), sorted(s)) if c != d)`
`return filter(None, map(lambda c, d: c != d and c, sorted(t), sorted(s)))[0]`

• @StefanPochmann great job Stefan, never thought I could use `next()` in that way. Smart.

• @agave Don't you import collections in you 2 method???

• @agave and this is my 1 line:

``````class Solution(object):
def findTheDifference(self, s, t):
return [i for i in t if i not in s or s.count(i)!=t.count(i)][0]

``````

• @StefanPochmann Can you please explain in a detailed way

• @Gene20 i like ur answer

• I used the second solution and this is what I got when I hit submit:

Input:"abcd"
"abcde"
Output:"e"
Expected:'e'

... pretty wierd.

• @chuuu It happens once in a previous contest... Do not even know how to trick the system to convert "e" to 'e'.

• @chuuu I got the same problem. Did you solve it??

• @Yiyu Yeah still not solved. And this problem doesn't show in the "Run Code" submission.

• @lijiahongasdf Not yet, pls let me know if you can solve this.

• return next((c for c, d in zip(sorted(t), sorted(s)) if c != d), max(t))

Hi, shouldn't the max(t) be max(t,s)? Thanks

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