We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Its not exactly a fast solution but I used this same idea. Here is my python code for it. First I generate a dictionary with a k,v pair which is (length of substring, substrings of the same length). Then I generate all the possible substrings and for each one I sort them and them find out if the sorted substring is already in the array of sorted substrings of the same length. If so we've found another anagram so we increment the anagram counter. After generating all the substrings we will also have the number of anagrams.
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
Its not exactly a fast solution but I used this same idea. Here is my python code for it. First I generate a dictionary with a k,v pair which is (length of substring, substrings of the same length). Then I generate all the possible substrings and for each one I sort them and them find out if the sorted substring is already in the array of sorted substrings of the same length. If so we've found another anagram so we increment the anagram counter. After generating all the substrings we will also have the number of anagrams.