Solve Simple Java Annagrams Problem

  • Java
  • Thread starter akan
  • Start date
  • Tags
    Java
In summary, the conversation discusses a previous assignment to create an anagrams class in a computer science class. The class was able to create a HashMap of strings and hashsets for single words. The new assignment is to generalize the class to find anagrams of sentences without modifying the database. The suggested approach includes converting the input string into separate words and using the same method as the initial class to produce hashsets, then converting them into arrays and combining them to produce all possible combinations of anagrams. The conversation is then considered closed as the individual has found a solution.
  • #1
akan
60
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
  • #2
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.
 
  • #3
Thanks, but I've already found a solution. :)
The question may now be considered closed.
 

1. How do I solve a simple Java anagram problem?

To solve a simple Java anagram problem, you will need to first understand what an anagram is. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. To solve the problem, you will need to write a program that can identify and rearrange the letters of the given word or phrase to form new words or phrases. This can be done by using loops, arrays, and string manipulation techniques.

2. What is the best way to approach a Java anagram problem?

The best way to approach a Java anagram problem is to break it down into smaller, manageable steps. First, identify the given word or phrase and its individual letters. Then, create an array to store the letters and use a loop to iterate through the array and rearrange the letters to form new words or phrases. You can also use built-in string methods like substring() and charAt() to manipulate the strings.

3. How do I check if two words are anagrams in Java?

To check if two words are anagrams in Java, you can compare the lengths of the words to see if they have the same number of letters. Then, you can sort the letters of both words alphabetically using a sorting algorithm like Arrays.sort() and compare the sorted strings to see if they are equal. If they are equal, then the words are anagrams.

4. Can I use recursion to solve a Java anagram problem?

Yes, you can use recursion to solve a Java anagram problem. Recursion is a programming technique where a function calls itself repeatedly until a certain condition is met. In the case of anagrams, you can use recursion to generate all possible combinations of the given word or phrase and check if they are valid anagrams. However, it is important to ensure that your recursion has a base case to prevent infinite looping.

5. Are there any built-in methods in Java for solving anagrams?

No, there are no built-in methods in Java specifically for solving anagrams. However, there are built-in methods for string manipulation and sorting that can be useful in solving anagrams. It is up to the programmer to use these methods creatively to come up with an efficient solution for the anagram problem.

Similar threads

  • STEM Academic Advising
Replies
13
Views
2K
Replies
4
Views
3K
  • STEM Academic Advising
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top