Write the function getCommonLetters(word1, word2)
that takes in two words as arguments and returns a new string that contains letters found in both string. Ignore r
If you want correction to your solution, then the problem is that you return the first common letter that you find. You have to continue searching for common letters and combine them into a result:
def getCommonLetters(word1, word2):
res = ""
for letter in word1:
if letter in word2:
if letter not in res: # skip if we already found it
# don't return yet, but rather accumulate the letters
res = res + letter
return res
The solutions suggesting using set
can be faster, especially if you are checking long words.