Solve Simple Java Annagrams Problem

  • Context: Java 
  • Thread starter Thread starter akan
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The discussion centers on enhancing a Java class designed to find anagrams by extending its functionality to handle sentences instead of just single words. The initial implementation utilizes a HashMap mapping sorted letter sequences to HashSets of corresponding anagrams. Participants suggest a method involving string manipulation, HashSet utilization, and combination generation to achieve the desired output of sentence anagrams. The conversation concludes with the original poster indicating they have found a solution, thus closing the question.

PREREQUISITES
  • Java programming knowledge, particularly with HashMap and HashSet.
  • Understanding of string manipulation techniques in Java.
  • Familiarity with generating combinations of elements in arrays.
  • Basic knowledge of anagram algorithms and their implementation.
NEXT STEPS
  • Research Java string manipulation methods for efficient word processing.
  • Explore advanced techniques for generating combinations in Java.
  • Study the performance implications of using HashMap and HashSet for large datasets.
  • Investigate existing libraries or frameworks that facilitate anagram generation.
USEFUL FOR

Java developers, computer science students, and anyone interested in algorithm design and optimization for anagram-related problems.

akan
Messages
58
Reaction score
0
[SOLVED] Simple Java Problem

Hello,

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:
Technology news on Phys.org
I'm not sure about the right way but this is how i'd do it:

1. Convert the input string into separate 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.
 
Thanks, but I've already found a solution. :)
The question may now be considered closed.
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K