Using Python, find anagrams for a list of words

前端 未结 22 905
失恋的感觉
失恋的感觉 2020-12-13 01:11

If I have a list of strings for example:

[\"car\", \"tree\", \"boy\", \"girl\", \"arc\"...]

What should I do in order to find anagrams in t

22条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 01:23

    Simple Solution in Python:

    def anagram(s1,s2):
    
        # Remove spaces and lowercase letters
        s1 = s1.replace(' ','').lower()
        s2 = s2.replace(' ','').lower()
    
        # Return sorted match.
        return sorted(s1) == sorted(s2)
    

提交回复
热议问题