An anagram is a word obtained by reordering the letters of another word. For exa
ID: 3589172 • Letter: A
Question
An anagram is a word obtained by reordering the letters of another word. For example, “once” and “cone” are anagrams. Note that each letter must be reused exactly once, so anagrams must have the same number of letters.
Which word in the English language has the most anagrams? Let us take “words in the English language” to mean words in the file “words.txt”, which is posted on Moodle.
Write a program called MostAnagrams to find the word with the most anagrams. Your program should take no input and produce a single integer, the maximum number of anagrams, as output.
For this problem you may find the String method toCharArray(), the String constructor that takes a character array argument, and the method java.util.Arrays.sort() useful.
Note: Have to use the starter code given by the professor (Cannot change the class name nor the methods in the starter code
public class WordPair private String word; private String srtword; public WordPair(String wd) word wd; char [] ca = wd.toCharArray(); java.util.Arrays.sort (ca); public String getSorted) return srtword; public String getUnsorted) return word;Explanation / Answer
public class AnagramSolver {
static Dictionary dictionary;
String temp=" ";
public AnagramSolver(Dictionary d)
{
this.dictionary=d;
}
public static String solve(String s)
{
double before = System.currentTimeMillis();
dictionary.contains(s);
double after = System.currentTimeMillis();
if(dictionary.contains(s))
return s;
else
return null;
}
public static void main (String args[]) throws Exception
{
if(args[2].equals("b"))
{
BinaryDictionary b=new BinaryDictionary(args[1]); // Click here Binary Dictionary code
AnagramSolver as=new AnagramSolver(b);
}
else if (args[2].equals("l"))
{
LinearDictionary l=new LinearDictionary(args[1]); // Click here Linear Dictionary code
AnagramSolver as=new AnagramSolver(l);
}
else if(args[2].equals("h"))
{
HashDictionary h=new HashDictionary(args[1]); // Click here Hash Dictionary code
AnagramSolver as=new AnagramSolver(h);
}
permute(args[0]);
}
public static void permute( String input)
{
System.out.println("Original/Input word is :" +input);
int inputLength = input.length();
boolean[ ] used = new boolean[ inputLength ];
StringBuffer outputString = new StringBuffer();
char[ ] in = input.toCharArray( );
doPermute ( in, outputString, used, inputLength, 0 );
}
public static void doPermute ( char[ ] in, StringBuffer outputString,
boolean[ ] used, int inputLength, int level)
{
if( level == inputLength) {
String temp=solve(outputString.toString());
if(temp!=null)
{
System.out.println("");
System.out.println("congratulations!!!! " +temp.toUpperCase()+" is the word in the dictionary");
System.exit(0);
}
return;
}
for( int i = 0; i < inputLength; ++i )
{
if( used[i] ) continue;
outputString.append( in[i] );
used[i] = true;
doPermute( in, outputString, used, inputLength, level + 1 );
used[i] = false;
outputString.setLength( outputString.length() - 1 );
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.