Writing JAVA program help World-renowned Prof. A. N. Agram\'s current research d
ID: 3815244 • Letter: W
Question
Writing JAVA program help
World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in English language texts. Given such a text, you are to find the largest anagram groups.
A text is a sequence of words. A word w is an anagram of a word v if and only if there is some permutation p of character positions that takes w to v. Then, w and v are in the same anagram group. The size of an anagram group is the number of words in that group. Find the anagram groups.
Input Specification:
The input contains words composed of lowercase alphabetic characters, separated by whitespace. It is terminated by EOF.
Output Specification:
Output the anagram groups. Sort the groups by decreasing size. For each group output, print its size and its member words. Sort the member words lexicographically and print equal words only once.
Sample Input:
undisplayed
trace
tea
singleton
eta
eat
displayed
crate
cater
carte
caret
beta
beat
bate
ate
abet
Sample Output:
Group of size 5: caret carte cater crate trace .
Group of size 4: abet bate beat beta .
Group of size 4: ate eat eta tea .
Group of size 1: displayed .
Group of size 1: singleton .
Hints:
Your program will start by asking for “input file name” (Assume the file is in the same directory as your code):
o Use Exception handling for input file error (i.e., if file does not exist)
Assume all words are lowercase
Your input files will not have more than 50 words.
Your program will be tested using multiple input files. Thus, implement a do_while loop to allow for multiple executions.
Explanation / Answer
/**
*
* World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in English language texts. Given such a text, you are to find the largest anagram groups.
A text is a sequence of words. A word w is an anagram of a word v if and only if there is some permutation p of character positions that takes w to v. Then, w and v are in the same anagram group. The size of an anagram group is the number of words in that group. Find the anagram groups.
Input Specification:
The input contains words composed of lowercase alphabetic characters, separated by whitespace. It is terminated by EOF.
Output Specification:
Output the anagram groups. Sort the groups by decreasing size. For each group output, print its size and its member words. Sort the member words lexicographically and print equal words only once.
Sample Input:
undisplayed
trace
tea
singleton
eta
eat
displayed
crate
cater
carte
caret
beta
beat
bate
ate
abet
Sample Output:
Group of size 5: caret carte cater crate trace .
Group of size 4: abet bate beat beta .
Group of size 4: ate eat eta tea .
Group of size 1: displayed .
Group of size 1: singleton .
Hints:
Your program will start by asking for “input file name” (Assume the file is in the same directory as your code):
o Use Exception handling for input file error (i.e., if file does not exist)
Assume all words are lowercase
Your input files will not have more than 50 words.
Your program will be tested using multiple input files. Thus, implement a do_while loop to allow for multiple executions.
*
*
*
* VI336563
* TODO
*/
package com.vimal.string.hard;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* @author VI336563
*
*/
public class Anagram {
/**
* @param argsvoidVI336563
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
String sCurrentLine=null;
ArrayList<String> wordlist = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("D:/file.txt")); // give whatever path you wnat to give
// read the text input file
while (( sCurrentLine = br.readLine()) != null) {
// System.out.println(sCurrentLine);
//Arrays.sort(sCurrentLine.toCharArray());
wordlist.add(sCurrentLine); // store all words in list.
}
HashMap<String,List<String>> map =new HashMap<String,List<String>>();
for(String word:wordlist)
{
char[] wordArray = word.toCharArray();
Arrays.sort(wordArray); //sort each word-----because if after sorting two strings are equal that means they are anagranms
List<String> tempList = map.get(new String(wordArray));
if(tempList == null) tempList = new ArrayList<String>();
tempList.add(word);
map.put(new String(wordArray),tempList); // store same words in Map as a list of string
}
StringBuilder result = new StringBuilder();
List<List<String>> allTheLists = new ArrayList<List<String>>();
for(Map.Entry<String, List<String>> entry : map.entrySet())
{
allTheLists.add(entry.getValue()); // now get the list of all anagram string list
}
// sort the list as per the size of each list of string--------------use Compare method to comapre two objects
Collections.sort(allTheLists, new Comparator<List>()
{
public int compare(List a1, List a2) {
return a2.size() - a1.size(); // assumes you want biggest to smallest
}
});
for(List<String> li:allTheLists)
{
System.out.println("Group of size "+li.size()+":"+li);
}
result.toString();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.