To get the common letters in two words without repetition

前端 未结 4 690
北海茫月
北海茫月 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:03

    You can use set intersection

    >>> ''.join(set('apple').intersection(set('google')))
    'el'
    

    The function can be defined as

    def getCommonLetters(a, b):
        return ''.join(sorted(set(a).intersection(set(b))))
    

    Example

    >>> def getCommonLetters(a, b):
    ...         return ''.join(sorted(set(a).intersection(set(b))))
    ... 
    >>> getCommonLetters('google','apple')
    'el'
    

提交回复
热议问题