To get the common letters in two words without repetition

前端 未结 4 684
北海茫月
北海茫月 2021-01-29 10:29

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

4条回答
  •  抹茶落季
    2021-01-29 11:21

    you can use set() and set intersection to find the common elements of two sets -

    def getCommonLetters(word1, word2):
        return ''.join(sorted(set(word1) & set(word2)))
    

    & is for set intersection .


    Example/Demo -

    >>> def getCommonLetters(word1, word2):
    ...     return ''.join(sorted(set(word1) & set(word2)))
    ...
    >>> getCommonLetters('apple','google')
    'el'
    >>> getCommonLetters('microsoft','apple')
    ''
    

提交回复
热议问题