-5

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"]
4
  • 1
    What is the expected output for the given strings ?
    – sushanth
    Jul 12, 2020 at 9:55
  • @Sushanth after removing common letters in both strings, racecup becomes "cecup" and alarm becomes "lam".
    – TechBee
    Jul 12, 2020 at 9:59
  • 2
    Why doesn't alarm become lm? Jul 12, 2020 at 9:59
  • 1
    Does this answer your question? Remove common letters in strings
    – bad_coder
    Jul 12, 2020 at 14:03

3 Answers 3

0

There are better ways to do this in python

str1="racecup"
str2="alarm"

common = set(str1).intersection(set(str2))

print(common)

str1 = ''.join([i for i in str1 if i not in common])
str2 = ''.join([i for i in str2 if i not in common])

print("str1 - " + str1)
print("str2 - " + str2)

Output:

{'a', 'r'}
'cecup'
'lm'
1
  • @AleksanderIkleiw, I think somebody is messing with both of your accounts, don't worry, Stackoverflow will automatically reverse the serial voting. Jul 12, 2020 at 10:21
0
#!/usr/bin/python3

def remove_common_letters(string1, string2):
    cleaned_string1 = [letter for letter in string1 if letter not in string2]
    cleaned_string2 = [letter for letter in string2 if letter not in string1]
    print("STRING1: {0} - Without common letters: {1}".format(string1, cleaned_string1))
    print("STRING2: {0} - Without common letters: {1}".format(string2, cleaned_string2))

if __name__ == '__main__':
    remove_common_letters('racecup', 'alarm')


STRING1: racecup - Without common letters: ['c', 'e', 'c', 'u', 'p']
STRING2: alarm - Without common letters: ['l', 'm']

Then you can simply join the list into a string. with ''.join

0

Use the inter() function for finding the common characters and using the replace function from the string class, these characters can be replaced.

def inter(lst1, lst2): 
    lst3 = [value for value in lst1 if value in lst2] 
    return lst3 
str1="racecup"
str2="alarm"
s1 = list(str1)
s2 = list(str2)
common = inter(s1,s2)
for i in common :
    str1 = str1.replace(i,'',1)
    str2 = str2.replace(i,'',1)

Does this work?

Not the answer you're looking for? Browse other questions tagged or ask your own question.