Project Euler 98 - Anagramic Squares

Official link:

Thought Process

  1. Go through the words and keep track of all the words that are anagrams to each other, I also keep track of the length of the anagramic words, this way when I loop through them I will start from biggest to smallest

    • I check this by using sorted(list(a_string)) for example sorted(list(CARE)) = [A, C, E, R]

  2. Generate all square numbers < 10^(number of digits in longest word)

    • For this problem it is 9, ('INTRODUCE', 'REDUCTION')

  3. Then I go through the candidates words and then the square numbers

    • Check words that are same length as square numbers, then I check if that number and word are a "match" that is I can uniquely match each letter to a distinct integer and when I replace the all letters with their number I get the given square number

      1. If they are a match, I check if the anagramic pair is also a square number. I do this by replacing the letters with our distinct integer's and test if the new number is a square

        1. If it is, this means, since we are going backwards from longest words, that the first pair we find that satisfies all the above properties will be the correct answer, so I just need to return the maximum square between the 2 words

Interactive Code

No interactive code for this problem, my code is given below. It is a bit messy so please read carefully