View the step-by-step solution to:

Anagram solver help using recursive backtracking Okay so I have to make a class that takes a string passed in and solves all of the anagrams for that...


Anagram solver help using recursive backtracking

Okay so I have to make a class that takes a string passed in and solves all of the anagrams for that phrase(I just have
to write the class that handles the main, as the instructor already wrote the main method in a class for us. I am a little lost on where to go after getting past the constructor. We have to write the following methods:

public Anagrams(Set<String> dictionary)

In this constructor you should initialize a new anagram solver over the given dictionary of words. You may assume that

the words in the set are in alphabetical order. Do not modify the set passed to your constructor.

You should throw an IllegalArgumentException if the set passed is null.



public Set<String> getWords(String phrase)

In this method you should return a set containing all words from the dictionary that can be made using some or all of the

letters in the given phrase, in alphabetical order. For example, if your anagram solver is using the dictionary

corresponding to dict1.txt and you are passed the phrase "Barbara Bush", you should return a set containing the

elements [abash, aura, bar, barb, brush, bus, hub, rub, shrub, sub].

You should throw an IllegalArgumentException if the string is null.



public void print(String phrase)

In this method you should use recursive backtracking to find and print all anagrams that can be formed using all of the

letters of the given phrase, in the same order and format as in the example log on the previous page. For example, if your

anagram solver is using the dictionary corresponding to dict1.txt and you are passed the phrase "hairbrush", your

method should produce the following output:

[bar, huh, sir]

[bar, sir, huh]

[briar, hush]

[huh, bar, sir]

[huh, sir, bar]

[hush, briar]

[sir, bar, huh]

[sir, huh, bar]

You should throw an IllegalArgumentException if the string is null. An empty string generates no output.



public void print(String phrase, int max)

In this method you should use recursive backtracking to find and print all anagrams that can be formed using all of the

letters of the given phrase and that include at most max words total, in the same order and format as in the example log on

the previous page. For example, if your anagram solver is using the dictionary corresponding to dict1.txt and this

method is passed a phrase of "hairbrush" and a max of 2, your method should produce the following output:

[briar, hush]

[hush, briar]

If max is 0, print all anagrams regardless of how many words they contain. For example, if using the same dictionary and

passed a phrase of "hairbrush" and a max of 0, the output is the same as that shown earlier on this page with no max.

You should throw an IllegalArgumentException if the string is null or if the max is less than 0. An empty string

generates no output.

The provided AnagramMain program calls your methods in a 1-to-1 relationship, calling getWords every time before

calling print. But you should not assume any particular order of calls by the client. Your code should still work if the

methods are called in any order, any number of times.


We also have a helper class to use to help us accomplish this. It contains the following methods:


public LetterInventory(String s) --- constructs a letter inventory for the given string


public void add(LetterInventory li)

public void add(String s)  --- adds the letters of the given string/inventory to this one


public boolean contains(LetterInventory li)

public boolean contains(String s)  --- returns true if this inventory contains all letters at least as

many times as they appear in the given string/inventory


public boolean isEmpty() --- returns true if the inventory contains no letters


public int size() --- returns the total number of letters in the inventory


public void subtract(LetterInventory li)

public void subtract(String s) --- removes letters of the given string/inventory from this one;

throws IllegalArgumentException if not contained


public String toString() --- string version of inventory, such as "[eehhllort]"


so far it is what I have, and I am not sure how to connect with the letterInventory class what is provided to us.. Can you give me a big picture of what should I do to this assignment ? thank you !

public class Anagrams {

    public static Set<String> dictionary;


public Anagrams(Set<String> dictionary){
    this.dictionary = dictionary;
}

public Set<String> getWords(String phrase){

    Set<String> anagrams = new TreeSet<String>();
    anagrams.addAll((getWords(phrase)));
    if(phrase==null){throw new IllegalArgumentException();
    }else{for(int i =0;i<phrase.length();i++){
        String s1 = phrase.substring(i,i+1);
        String temp = phrase.substring(0,i)+(i+1);
        anagrams.addAll(getWords(phrase+s1));

    }

    }

}
public void print(String phrase){
    if(phrase==null) {
        throw new IllegalArgumentException();
    }

}
public void print(String phrase, int max){

}
}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question