Anagrams

Two strings are considered anagrams if the characters of one string can be rearranged to form the second string. For instance, "cat" and "act" are anagrams because the letters of "cat" can be rearranged to spell "act". "cat" and "cat" are also anagrams of each other.

This file contains a line-separated list of strings. Let A be the number of strings in the file that are an anagram of another string in the file. What is A ?

Click here to open the file.


The answer is 59.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Discussions for this problem are now closed

Owen Reiss
May 13, 2014

Here's a solution in Haskell...

strings = ["kepqerrvjdmiorrno", "rbisaumvy", "tzoinreyxhzfy", ... "gyknmvdhllqjhxc", "nhlxexyjg", "errarydod", "ydwlkyiuwwxrmv", "irvilze", "ohhwbvrna"]
a = length . concat . filter (\str -> length str > 1) . group . sort . map sort $ strings

Given a list of the strings, sort each string alphabetically, sort the whole list alphabetically, group equal strings together into a nested list, weed out the lists that only have one element (no anagrams), then flatten all the strings into a single list, and get the length of that list!

The answer is 59 .

We want to count all words that are anagrams of another word in the file. This can be done in linear time using two loops through the list of words.

On the first loop, create a lookup table of counts for each anagram group. To figure out which anagram group a word belongs to, alphabetize its letters. This is an effective lookup key because all anagrams of a word alphabetize to the identical string.

Start the answer count at 0. On the second loop, alphabetize the letters of each word and look up its anagram group in the lookup table. If the count is greater than 1, then this word has an anagram elsewhere in the file, so increment the answer count.

At the end of the second loop, return the answer.

# Python example
def anagrams():
    with open('cs_anagrams.txt', 'r') as f:
        anagrams = []
        words = []
        counts = {}

        # iterate through the words
        # transform each word into its alphabetized anagram
        # store a count of appearances of each alphabetized anagrams
        for word in f:
            s = ''.join(sorted(word.rstrip()))
            words.append(s)
            if (s in counts):
                counts[s] += 1
            else:
                counts[s] = 1

        # iterate through the words again 
        # transform each word into its alphabetized anagram
        # lookup alphabetized anagram count in the counts lookup
        # increment answer for each word whose count is more than 1
        answer = 0
        for w in words:
            s2 = ''.join(sorted(w.rstrip()))
            if counts[s2] > 1:
                answer += 1

        print answer
Elkio deAtn
May 17, 2014

Step 1: Create an array that contains all the strings:

var s= new Array(); s.push("kepqerrvjdmiorrno"); s.push("rbisaumvy "); etc... //code is in javaScript

Step 2: Sort each string alphabetically:

 A special function created to do so:

    function sortString(z){  //sorts the string alphabetically
        var v=new Array();
        for(var i=0;i<z.length;i++){
            v.push(z.charAt(i));
        }
        v.sort();
        return v.join("");
    }
 And this has to be applied to every element in "s":

for (var i=0;i<s.length;i++){

    s[i]=sortString(s[i]);

}

Step 3: For each element in "s", see if there is "at least 1" equal element :

var countAnagrams=0;
    for (var i=0;i<s.length;i++){
        for (j=0;j<s.length;j++){

            if (j !=i){
                if (s[j]==s[i]){
                    countAnagrams++;
                    document.writeln(countAnagrams +":"+s[j]+ "<br />");
                    break;//next i, exit for j.


                }


            }
        }

    }

countAnagrams is the answer (59).

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...