Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* * This method determines whether or not a given String is an anagram of some

ID: 3873756 • Letter: #

Question

/*

* This method determines whether or not a given String is an anagram of some subset of the

* letters in the ArrayList<Character>.

*

* See:

* http://www.dictionary.com/browse/anagram

*

* The basic idea here is that we'll loop through each character in word, and remove each word from

* the ArrayList<Character>. The remove method of the ArrayList removes ONE occurrence from the

* list.

*

* Example: Suppose list is the ArrayList<String> that prints as [b, o, o, k, k, e, e, p, e, r]

* then list.remove('e') changes list to [b, o, o, k, k, e, p, e, r].

* Calling list.remove('e') again changes list to [b, o, o, k, k, p, e, r].

*

* The remove method returns a boolean value. If the call changes the contents of the ArrayList the

* method returns true. If calling the method does not change the ArrayList then the method

* returns false.

*

* HINT: because this method will remove characters from ArrayList<Character> it is working with,

* it is important to make a copy of what's in reference before using it. Write a loop that copies

* the contents of reference to a new ArrayList<Character>.

*

*/

public boolean anagramOfLetterSubset(String word, ArrayList<Character> reference) {

// put your code here

return false; // change the value returned

}

/*

* This method takes a word (a String) and a dictionary of words (an ArrayList<String>) and returns

* a collection of words (a HashSet<String>) that are anagrams of some subset of the letters in word.

*

* Put another way, this method finds all the words or length at least 2 that can be played from the

* letters in word.

*

* HashSet is a collection that, for our purposes in this homework, behaves like an ArrayList with

* the following exception:

* calling add(X) on a HashSet adds X only if X is not already in the collection

* In other words, HashSet does not allow duplicate entries. Because HashSet does not allow duplicates

* we get unique words in the result.

*

* HINT: in defining this method you should find a natural use for both string2charList and also

* anagramOfLetterSubset.

*/

public HashSet<String> possibleWords(String word, ArrayList<String> dictionary) {

HashSet<String> words = new HashSet<String>();

// put your code here

return words;

}

Explanation / Answer

public boolean anagramOfLetterSubset(String word,

ArrayList<Character> reference) {

ArrayList<Character> refCopy = new ArrayList<>();

for (char c : reference) {

refCopy.add(c);

}

// check for all characters of word

for (char letter : word.toCharArray()) {

if (refCopy.remove(letter) == null) {

// the current character is not present in the arraylist

return false;

}

}

// all the characters were present, hence return true

return true; // change the value returned

}

public ArrayList<Character> string2charList(String s) {

ArrayList<Character> chars = new ArrayList<>();

for(char c: s.toCharArray()) {

chars.add(c);

}

return chars;

}

public HashSet<String> possibleWords(String word,

ArrayList<String> dictionary) {

HashSet<String> words = new HashSet<String>();

// for each word in dict

for(String dictWord : dictionary) {

if(anagramOfLetterSubset(dictWord, string2charList(word))) {

words.add(dictWord);

}

}

  

// put your code here

return words;

}