__author__ = 'nickyuan'
'''
Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.
'''


class Solution(object):
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        if set(s) == set(t):
            return ''.join(set(s))
        else:
            return ''.join(set(s)^set(t))


'''
用set方法不容易实现，因为有很多特例
比如 s = 'a' t = 'aa' ，则输出应该为'a'，用set取交集就会输出空
比如 s = 'ae' t = 'aea'， 则输出应该为'a'，用set取交集就会为'ae'
那么该用什么办法呢？
注意：set转化为string，需要用''.join
'''
class Solution1(object):
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        sum1 = sum(map(ord, [c for c in s]))
        sum2 = sum(map(ord, [c for c in t]))
        return chr(sum2 - sum1)

'''
ord()函数主要用来返回对应字符的ascii码，chr()主要用来表示ascii码对应的字符他的输入时数字，可以用十进制，也可以用十六进制。

例如：print ord('a) #97

     print chr(97) #a
'''

'''
map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. 
If additional iterable arguments are passed, function must take that many arguments 
and is applied to the items from all iterables in parallel. 
If one iterable is shorter than another it is assumed to be extended withNoneitems. 
If function isNone, the identity function is assumed; 
if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items 
from all iterables (a kind of transpose operation). 
The iterable arguments may be a sequence or any iterable object; the result is always a list.

一点一点看：

1、对可迭代函数'iterable'中的每一个元素应用‘function’方法，将结果作为list返回。
'''
'''
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]
'''