Checking strings against each other (Anagrams)

后端 未结 23 1984
忘了有多久
忘了有多久 2020-11-30 10:55

The assignment is to write a program that accepts two groups of words from the user and then prints a \"True\" statement if the two are anagrams (or at least if all the lett

23条回答
  •  难免孤独
    2020-11-30 11:09

    Why not just sort the strings?

    >>> sorted('anagram')
    ['a', 'a', 'a', 'g', 'm', 'n', 'r']
    >>> sorted('nagaram')
    ['a', 'a', 'a', 'g', 'm', 'n', 'r']
    >>> sorted('anagram') == sorted('nagaram')
    True
    

提交回复
热议问题