-4

I have a java program that

  • reads a text file,
  • puts all it's words in an ArrayList
  • puts all the words into an ArrayList, lowercase with punctuation removed

I now want to make two more things.

  • A function that creates all the anagrams of the strings of a String ArrayList,
  • an ArrayList of ArrayLists that will store each of the anagrams and the original string into each ArrayList in the ArrayList.

So I want to develop a function that will take a string that I am inserting from one ArrayList into a new ArrayList and make all it's anagrams and put them in an ArrayList and then put that ArrayList in the ArrayList that is reading the old ArrayList.

Something that will look like this:

List<String> arLists = new ArrayList<String>(); //makes new array list
    for(String arList : words) //takes values from old array list
        ArrayList<String> anaLists = new ArrayList<String>(); //makes a new array list
        arLists.add(anag(anaLists,arList,"")); //uses a function that makes an 

I want to make a function kinda like this, but what I have made here... doesn't really work.

public void anag(ArrayList<String> anaLists, String s1, String s2){
    if(s1.length() == 0){
        return anaLists;
    }
    for(int i = 0 ; i < s1.length() ; i++){ //only runs for string length
        String anaList = anag(s1.substring(0, i) + s1.substring(i+1, s1.length()), s1.charAt(i) + s2);
        anaLists.add(anaList);
        }
    }

Some guidance on this would be superb.

Patashu
  • 21,443
  • 3
  • 45
  • 53
Ryan Tibbetts
  • 412
  • 1
  • 8
  • 22
  • Yo dawg, we put an `ArrayList` in your `ArrayList` so you can `.add` while you `.add` – Patashu Mar 13 '13 at 04:26
  • To all the people that down voted this. If you don't like how I'm saying it, then correct me or better yet help me make this post more understandable if you don't understand what I'm asking. Thanks. – Ryan Tibbetts Mar 13 '13 at 04:27
  • 2
    No need to put your complete question in **title**. There is a lot of space in text area below the title textbox – Apurv Mar 13 '13 at 04:29

2 Answers2

0

After some struggle, I have tried to understand your question and here's my answer. Correct me if I am wrong. First of all, you can do all that pre-processing stuff like changing case, removing grammar using one array list. Now to the actual function:

public void getAnag(String baseStr, ArrayList<String> finalAnagList)
{
    ArrayList<String> anagList = new ArrayList<String>();
    anagList = getAnagrams(baseStr); // getAnagrams is a support function to get the anagrams
    anagList.add(baseStr); // I suppose you want to add the base string also to the anagrams list
    finalAnagList.add(anagList);
}

And your calling function in program would be:

public void testAnagrams()
{
    ArrayList<String> words = getWordsFromFile("/home/list.txt"); // gets the words from the file
    ArrayList<String> anagramsList = new ArrayList<String>();
    foreach(String word : words)
    {
         getAnag(word, anagramsList);
    }
}
Rakesh K
  • 8,237
  • 18
  • 51
  • 64
  • The idea of the program that I have is to show someone viewing it the steps for which I am taking to get the anagrams of the words taken from a file as well as the original words. So along the way I've made multiple lists for printing reasons at the end of my code. – Ryan Tibbetts Mar 13 '13 at 04:47
  • @RyanTibbetts Yes, I got that, and so my answer tries to simplify the overall structure of the program. You can see that I have not written the function for finding the anagrams itself - hope you can come up with that ;) – Rakesh K Mar 13 '13 at 04:50
  • I have a function already that makes anagrams, but I do not know how to put each anagram into a position in the arraylist that I want to put into another arraylist over and over. – Ryan Tibbetts Mar 13 '13 at 04:59
0

To make all anagrams from a string, follow the following steps:

Step 1: Use String's replace to remove whitespace, and make sure all punctuation and capitalization has been removed.

Step 2: Write this function f(string s, string anagram, ArrayList<String> array) and call it with s = yourstring, anagram = "", array = new ArrayList<String>():

If s is empty, add anagram to array and return
For each letter l in s:
    newanagram = anagram + l
    news = s with l taken out of it (e.g. make a substring of everything before l in s and everything after l in s, and concatenate them together)
    call f(news, anagram, array)

This will explore a 'tree' of recursive self calls, and at each 'leaf' of the 'tree' each possible permutation of all the letters will be added to the array. When it finishes, n*n-1*n-2*n-3... aka n factorial entries will be in the array, which is how you know you're on the right track :)

And if you need an anagram of every string in an arraylist, just call it in a for loop.

Patashu
  • 21,443
  • 3
  • 45
  • 53