Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

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