reset password
Author Message
dtang9
Posts: 52
Posted 14:08 Sep 21, 2018 |

The other forum topic was getting full, so I will ask here.

For the recursive method that takes a String and returns a list of Strings of all anagrams, it has a "loop that generates each possible substring consisting of all but one character in the input String, ie the substring that omits the first letter, then the substring that omits the second letter, etc. Within the loop, the method calls itself recursively for each substring."

I tried to do this on paper for a String "abc". I got bc and c as substrings.

"For each String in the list returned by the recursive call, add the omitted character back to the end and add the String to the list to be returned."

With my substrings bc and c, I got: bca and cb

"When the first instance of this method returns, the list will contain all anagrams of the original input String"  and the base case is to "return a list containing only a new empty String if the input String has length 0".

When the first instance returns, does it mean that the base case stopped the recursive call and the method is finished?

Also, I am confused on how to make anagrams, can you provide an example of all anagrams of a String like "abc"? For "abc", can anagrams be one, two, and three letter words?

Is there any specific examples in the lecture note slides to help me understand it?

jhurley
Posts: 207
Posted 15:44 Sep 21, 2018 |

if the original string is "abc", the loop should generate "bc", "ac", and "ab", and make recursive calls using those strings. when the recursive call returns, add the letter that was omitted back to the beginning of each of the strings in the new list.

dtang9
Posts: 52
Posted 16:48 Sep 22, 2018 |

For the recursive method that returns a list of anagrams, I am not sure how to omit a letter in the String. I only get some possible anagrams.

For starting the nonrecursive filter method, I made the list of anagrams into an array and put those values into a list.  Using this list in the recursive filter method, I am not sure how to compare it with the valid word list String by String.

Last edited by dtang9 at 16:49 Sep 22, 2018.
jhurley
Posts: 207
Posted 17:58 Sep 22, 2018 |

String has methods charAt() and substring() that can get a character by index and get subtrings using indices.  Use a loop with a counter.  Each time through, get the substring consisting of all characters before the one whose index is the loop counter and concatenate it with the one consisting of all characters after that.  Keep a copy of the character you omitted and add it bck to the beginning of each string after the recursion unwinds.