Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Last active February 1, 2022 18:31
Show Gist options
  • Save jatinsharrma/99fd8fc644c7343fad0b74ee73dc99a4 to your computer and use it in GitHub Desktop.
Save jatinsharrma/99fd8fc644c7343fad0b74ee73dc99a4 to your computer and use it in GitHub Desktop.
Write a python program to display all the common characters between two strings. Return -1 if there are no matching characters.
# Write a python program to display all the common characters between two strings. Return -1 if there are no matching characters.
def find_common_characters(msg1,msg2):
result = []
if len(msg1) > len(msg2):
result = my_func(msg1,msg2)
else:
result = my_func(msg2,msg1)
if len(result) == 0:
return -1
else:
result = "".join(result)
return result.replace(" ","")
def my_func(bigger,smaller):
length = len(smaller)
bigger =list(bigger)
smaller = list(smaller)
i = 0
result = []
while i < length :
ans = smaller[i] in bigger
if ans == True:
result.append(smaller[i])
bigger.remove(smaller[i])
i = i + 1
return result
msg1="abababasdsd"
msg2="asdscsdw"
common_characters=find_common_characters(msg1,msg2)
print(common_characters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment