Checking strings against each other (Anagrams)

后端 未结 23 1980
忘了有多久
忘了有多久 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:15

    This worked for me

    str1="abcd"
    str2="bcad"
    word1=[]
    word2=[]
    for x in range(len(str1)):
        word1.append(str1[x])
    for x in range(len(str2)):
        word2.append(str2[x])
    if(len(word1)==len(word2)):
        for letter in word1:
            if letter in word2:
                word2.remove(letter)
    
    if len(word2)==0:
        print "anagram"
    else:
        print "not anagram"
    

提交回复
热议问题