I\'m making a word unscrambler in java. Right now I have a program that can print all rearrangements of 3 letters chosen from a word with 3 or more letters (no repeats). So for
Based on what @DrYap said, I wrote this (it's a little different than his):
Here is the code to start it off:
for(int i = 1; i <= len; i++)
{
newWords.add(new ArrayList());
loop(i);
}
Here is the loop method. A lot of the variables were declared as instance variables so I don't have to pass them into the parameter:
public static void loop(int level)
{
if (level == 0)
{
int pos = indices.size() - 1;
int pos2 = newWords.get(pos).size();
newWords.get(pos).add("");
for (Integer letIndex : indices)
{
String previous = newWords.get(pos).get(pos2);
newWords.get(pos).set(pos2, previous + lets.get(letIndex));
}
}
else
{
for (int i = 0; i < len; i++)
{
if (!indices.contains(i))
{
int indicesIndex = indices.size();
indices.add((Integer) i);
loop(level - 1);
indices.remove(indicesIndex);
}
}
}
}