Question

In: Computer Science

Problem 2: Two words are said to be anagrams if one can be formed by permuting...

Problem 2: Two words are said to be anagrams if one can be formed by permuting the letters of the other. For example: "pots", and "stop" are anagrams. An anagram chain is a list of words that are all anagrams to each other. The shortest anagram chain has length two. We are interested in calculating the length of the longest anagram chain in a given list of words. For example, the following nine words: rates, pots, tops, along, aster, stop, stare, tears, and long has two anagram chains where the longest includes the four words: rates, aster, stare, and tears.

Input

Your program will be tested on a number of test cases. The first line of the input file ("anagram.in") contains an integer D representing the number of test cases in the input file.

Each test case contains one or more words, but no more than 20000 words, with no duplicates. Each word appears on a separate line. All words are in small letters, and in no particular order. No word will be longer than 10 characters. Each test case ends with a string made of one or more '-' characters.

Output

For each test case, write, on a separate line, the length (number of words) of the longest anagram chain found in the given list of words. The output should be printed to the screen.

      Sample Input Sample Output:

2

rates                                                                                                                       4

pots

tops                                                                                                                        2

along

aster

stop

stare

tears

long

------

north

fresher

refresh

thorn

bye

--

Solutions

Expert Solution

Anagrams can be detected on the basis of the fact that they have the same characters the same number of times. Only the order is different. What is we made that order irrelevant? What if we shuffle all letters of a word in a fixed way? For example what if for each word we arrange the letters on the basis of alphabetical order. Then words which are anagrams would return in the same string.

Example - (cats, dogs, scat)

If we sort these three strings individually we get (acst, dgos, acst). Hence anagram words result in same strings.

Also, now if we sort this list of strings we would get (acst, acst, dogs) i.e. we would get all anagram words together which makes it easier for us to count.

This is the logic behind the algo we will apply.

The algo is as follows (after which i've implemented a C++ code)

  • Step 1 : Open the input file anagram.in
  • Step 2 : If file doesn't open show error
  • Step 3 : Read first line to get no of test cases (let s denote this). s so its converted to a number and stored in t
  • Step 4 : Take an empty list/array to store our sorted strings (let this be v)
  • Step 5 : Repeat the steps 6-13 t times (because we have t test cases)
  • Step 6 : v should be emptied (in case it's not because of a previous iteration)
  • Step 7 : Repeat steps 8-9 until a string starting with '-' is obtained (which denotes end of test case)
  • Step 8 : Read line by line from input file. We have two cases now
  • Step 9a : If line is a string starting with '-' then stop reading any more lines because it's end of test case.
  • Step 9b : If line is not a string strting with '-' then we sort the string and add it to our list/array v. Only one of steps 9a and 9b executes.
  • Step 10 : After all words are read and put into list/array v we sort the list/array v so that all anagram words collect together.
  • Step 11 : we take 2 variables to count current_maximum and overall_maximum number of anagram words.
  • Step 12 : we traverse the list/array v and keep increasing the current_maximum if we keep encountering same strings (i.e. anagrams). When we encounter two different words (i.e. not anagrams) we update overall_maximum (i.e overall_maximum = current_maximum if current_maximum > overall_maximum) and reset current_maximum to start counting from that word.
  • Step 13 : overall_maximum is our answer and hence it is displayed
  • Exit

Another method can be to store words in a data structure like SET / MAP (where a value can be inputted only once) and a count of words i.e. if a word is not present in set then put it in set and make its count 1. If a word is already present in set, increase it's count. Then just find the maximum count and that is our example.

Ex - (cats, dogs, scat, tacs, sogd)

we read cats, sort it to acst and push in set because not present before and its count is 1 now.

we read dogs, sort it to dgos and push it to set because not present before and its count is 1 now.

we read scat, sort it to acst and do not push in set because already present, only count increased to 2.

we read tacs, sort it to acst and do not push in set because already present, only count increased to 3.

we read sogd, sort it to dgos and do not push in set because already present, only count increased to 2.

maximum count is 3 hence answer is 3.

this method saves times and space both because we dont need to store all words, only one occurence of a word and we dont need to sort the whole list because we only have one occurence of a word.

Code for method 1 :

CODE FOR METHOD 2 :

first 3 steps and step 5-6-7-8-9 are same as previous method.

Hope this helped.


Related Solutions

Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
2. How many seven – letter code words can be formed using a standard 26 letter...
2. How many seven – letter code words can be formed using a standard 26 letter alphabet (12 points) a.         if repetition is allowed? b.         if repetition is not allowed? c.         if the first letter has to be a C and the rest of the letters are different? d.         if the first two letters have to be a vowel and the rest of the letters are different?
Find the number of five letter words that can be formed from the letters of the...
Find the number of five letter words that can be formed from the letters of the word PROBLEMS. ( please explain with details.) (a) How many of them contain only consonants? (b) How many of them begin and end in a consonant? (c) How many of them begin with a vowel? (d) How many contain the letter S? (e) How many begin with B and also contain S? (f) How many begin with B and end in a vowel? (g)...
In one well formed post to a discussion write a maximum of 350 words and express...
In one well formed post to a discussion write a maximum of 350 words and express the following. As an undergraduate student do you feel that the study of Macroeconomics theory is important and if so why?Explain one topic in macroeconomics (any topic)
Two words form a "metathesis pair" if you can transform one into the other by swapping...
Two words form a "metathesis pair" if you can transform one into the other by swapping two letters; for example, "converse" and "conserve". Write a function metathesis(word1, word2) to check if the two words can form a metathesis pair. The function will return a Boolean value (True or False). python code without using Zip method
two words that end in the same portion of letters but sound different are said to eye rhyme.
two words that end in the same portion of letters but sound different are said to eye rhyme. write a python program that prompts the user for an input and prints all the words that eye rhyme with the input, words with the same last three letters, use dictionary file
Question 2: In 400 words discuss about this issue, It has been said that if we...
Question 2: In 400 words discuss about this issue, It has been said that if we define death as ceasing to be alive, we must first define what it means to be alive. Discuss what you learned this week from the Jahi McMath case about the ethical challenges of determining death? Choose one of the definitions of death and make an argument to support that definition using ethical principles and concepts. Discuss how the use of this definition might impact...
a)     How many four-letter words can be formed from the letters of the word LAUNDRY if each...
a)     How many four-letter words can be formed from the letters of the word LAUNDRY if each letter can only be used one time in a word? Y is NOT considered a vowel in this word. b)    How many contain the letter Y? c)     How many contain both vowels? d)    How many of them contain exactly three consonants? e)    How many of them begin and end in a consonant? f)      How many begin with N and end in a vowel? g)     How many begin with N and...
A wire 370 in. long is cut into two pieces. One piece is formed into a...
A wire 370 in. long is cut into two pieces. One piece is formed into a square and the other into a circle. If the two figures have the same area, what are the lengths of the two pieces of wire (to the nearest tenth of an inch)?
A wire 370 in. long is cut into two pieces. One piece is formed into a...
A wire 370 in. long is cut into two pieces. One piece is formed into a square and the other into a circle. If the two figures have the same area, what are the lengths of the two pieces of wire (to the nearest tenth of an inch)?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT