题目
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.
难度
Easy
方法
字符串t
是字符串s
中的字符打乱后生成的,为了找出t
中不同的字符,可以采用异或的方法
python代码
class Solution(object):
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
i = 0
difference = 0
for i in xrange(len(s)):
difference ^= ord(s[i]) ^ ord(t[i])
return chr(difference ^ ord(t[-1]))
assert Solution().findTheDifference("abcd", "abcde") == "e"
assert Solution().findTheDifference("abcd", "dceba") == "e"
网友评论