在Python中使用不常见字符连接字符串?
这里给出了两个字符串,首先我们必须从第一个字符串中删除所有公共元素,并且第二个字符串的不常见字符必须与第一个字符串的不常见元素连接在一起。
示例
Input >> first string::AABCD Second string:: MNAABP Output >> CDMNP
算法
Uncommonstring(s1,s2) /* s1 and s2 are two string */ Step 1: Convert both string into set st1 and st2. Step 2: use the intersection of two sets and get common characters. Step 3: now separate out characters in each string which are not common in both string. Step 4: join each character without space to get a final string.
范例程式码
# Concatination of two uncommon strings
def uncommonstring(s1, s2):
# convert both strings into set
st1 = set(s1)
st2 = set(s2)
# take intersection of two sets to get list of common characters
lst = list(st1 & st2)
finallist = [i for i in s1 if i not in lst] + \ [i for i in s2 if i not in lst]
print("CONCATENATED STRING IS :::", ''.join(finallist))
# Driver program
if __name__ == "__main__":
s1 =input("Enter the String ::")
s2=input("Enter the String ::")
uncommonstring(s1,s2)输出结果
Enter the String ::abcde Enter the String ::bdkl CONCATEATED STRINGIS ::: acekl
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志