Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple Java Problem

  1. Apr 11, 2008 #1
    [SOLVED] Simple Java Problem


    In my computer science class, we were assigned to design an annagrams class, which would create a HashMap of String => HashSet, a sorted sequence of letters mapped to all annagrams of those letters found in the dictionary. This was done.

    Sample entries from the "database":
    opst => 'spot', 'pots', 'tops', 'stop';
    'act' => 'act', 'cat';

    Now, the new assignment is to generalize the class such that the class could find annagrams of sentences, not just single words. And the professor is suggesting that the mapping database should remain as-is, with no further modifications. Can someone please give a hint as to how this could possibly be done "the right way"?

    Sample of desired output:
    'computer science' => 'cement occupiers', 'ecumenic copters'.

    Thank you.
    Last edited: Apr 11, 2008
  2. jcsd
  3. Apr 17, 2008 #2
    I'm not sure about the right way but this is how i'd do it:

    1. Convert the input string into seperate words and store each word in a new string.
    2. Apply whatever method you used in your initial class to produce HashSets for each of these word strings.
    3. Convert these HashSets to arrays of Strings.
    4. Produce a new array of strings where each string is one possible combination of the annagrams in the right order.

    There's probably an easier way and i'm not sure if this helps either, perhaps some insight into how your initial class works, i mean maybe you wouldn't even need to split the string up into different words. Number 4 would probably be the most difficult to implement trying to get every combination especially with longer sentences. Perhaps it'd be easier to try and produce every combination for 2 words first and then extend the method to more when it works.
  4. Apr 17, 2008 #3
    Thanks, but I've already found a solution. :)
    The question may now be considered closed.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook