'''
题目
输入为两个字符串str1,str2。找出两个字符串中不同的字符
例子
None输入 -> None
'abc','abcc' -> 'c'
'abcd','abcde' -> 'e'
'aaabbcdd','abdbacade' -> 'e'
假设
两个字符串中只有唯一一个字符不同
所有字符均为ascii
tips
统计每个字符出现的次数
利用异或运算
'''
from functools import reduce
def find_diff(s1, s2):
if s1 is None or s2 is None:
return None
list_s2 = list(s2)
{list_s2.remove(x) for x in s1 if x in s2}
print(list_s2)
return ''.join(list_s2)
s1 = 'abc'
s2 = 'abcd'
from collections import Counter
c1 = Counter(s1)
c2 = Counter(s2)
print(c1)
print(c2)
print((c2-c1))
print(''.join(list(c2-c1)))
print('-----------------------------')
# print(reduce(lambda x, y: ord(x) ^ ord(y), s1+s2))
# print(s1+s2)
# 两个字符串中,只有一个唯一字符
def find_diff2(s1, s2):
s3 = s1 + s2
i = 1
temp = ord(s3[0])
while i < len(s3):
temp ^= ord(s3[i])
i += 1
# print('temp=',chr(temp))
return chr(temp)
def find_diff3(s1, s2):
# alist = [ord(x) for x in s1+s2]
# print(alist)
return chr(reduce(lambda x, y: x^y, [ord(x) for x in s1+s2]))
print(find_diff3(s1, s2))