Two approaches (2 lines) in Python


  • 0
    class Solution(object):
        def findTheDifference(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: str
            """
            from collections import Counter
            return (Counter(t) - Counter(s)).most_common(1)[0][0]
            
        def findTheDifference(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: str
            """
            from operator import xor
            return chr(reduce(xor, map(ord, t), reduce(xor, map(ord, s), 0)))
    

  • 0
    This post is deleted!

  • 0
    N

    my solution is

    class Solution(object):
        def findTheDifference(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: str
            """
            sum_s = sum(ord(i) for i in s)
            sum_t = sum(ord(i) for i in t)
            return chr(sum_t - sum_s)
    

    why is not work


  • 0

    @nimdanoob I tried your solution just now, it was accepted :) You may try it again.


Log in to reply
 

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