-2

Given a string, we must split it into two contiguous substrings, then determine the minimum number of characters to change to make the two substrings into anagrams of one another.

def anagram(s):
    flag = 0
    if len(s)%2 != 0: return -1
    else:
        temp1,temp2 = s[:len(s)//2],s[len(s)//2:]  
        temp1 = ''.join(sorted(temp1))
        temp2 = ''.join(sorted(temp2))

        for i in temp1:
            flag1 = temp2.count(i)
            if flag1>1: flag1 = 1
            else: flag1 = flag1
            flag += flag1
    return flag 

I'm sort of all over the place and highly complicating this without getting it right. Is there any way to simplify the logic?

srkdb
  • 775
  • 3
  • 15
  • 28

1 Answers1

0

To find all the anagrams of a word, I used the SOWPODS dictionary. Here is the link to download it: https://osdn.net/projects/sfnet_scrabbledict/downloads/Dictionary%20Files/sowpods.txt.gz/

def anagrams(s):
    text_file = open("sowpods.txt", "r")
    lines = text_file.readlines()
    words = [y.replace('\n','') for y in lines]
    result = [words[i] for i in range(0,len(words)-1) if ((''.join(set(sorted(s)))) == (''.join(set(sorted(words[i])))))]
    return result

In the file open command, it is preferred to give the link of your SOWPODS dictionary.