I want to eliminate the common letters in string1(str1
) and string2(str2
) like, suppose if string1 contains single "a", and string2 contains 2 "a's", then if elements get matched, then only one "a" of string2 must get eliminated and not both "a's",since there exists only one "a" in string1. I tried with the first code and it doesn't work out. so i slightly modified it and tried with my 2nd code, and yet it doesn't work out. I'm a beginner and i'm not good at english, so please answer me with your code. Thanks! in advance
first code:
str1="racecup"
str2="alarm"
temp1=str1;temp2=str2
str1=list(str1)
str2=list(str2)
while True:
flag=0
for i in str1:
for j in str2:
if i==j:
flag+=1
str1_ele_index=str1.index(i)
str2_ele_index=str2.index(j)
str1.remove(str1[str1_ele_index])
str2.remove(str2[str2_ele_index])
if flag==0:
break
print(temp1,"and",temp2,"have no letters in common")
print(temp1,":",str1,temp2,":",str2)
second code:
str1="racecup"
str2="alarm"
temp1=str1;temp2=str2
str1=list(str1)
str2=list(str2)
while True:
flag=0
for i in range(len(str1)):
for j in range(len(str2)):
if str1[i]==str2[j]:
flag+=1
str1.remove(str1[i])
str2.remove(str2[j])
if flag==0:
break
print(temp1,"and",temp2,"have no letters in common")
print(temp1,":",str1,temp2,":",str2)
Required output:
racecup and alarm have no letters in common
racecup:["c","e","c","u","p"] alarm:["l","a","m"]
alarm
becomelm
?