Last active
February 1, 2022 18:31
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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