Evo ti moj kod koji sam radio kad sam tako nešto isprobavao pa probaj da ga malo
prilagodiš svom zadatku. Pretpostavljam da će ti dati neku ideju ;)
Aj nek ti je sa srećom :D
Code:
import java.util.*;
public class Main{
public static void main(String[] args){
String[] source = {"a", "aba", "ab"};
ArrayList<String> result = Permutations.getPermutations(source);
for(String s : result){
System.out.print(s + " ");
}
System.out.println();
}
}
class Permutations{
public static ArrayList<String> getPermutations(String[] source){
ArrayList<String> list = new ArrayList<String>();
/* set assures to have only unique values */
Set<String> set = new TreeSet<String>();
/* loop for creating family of strings that start with same string */
for(int i = 0; i < source.length; i++){
/* initialize start of resulting String */
String temp = source[i];
/* contains number from which to add next strings in array */
int nextStart = i + 1;
if(nextStart == source.length){
nextStart = 0;
}
/* loop to make all Strings in family of strings
* that start with source[i] */
for(int k = 0; k < source.length; k++){
int stringToAdd = nextStart;
/* loop to add all consecutive strings to temp, if it reaches
* end of array starts from begining of array. Make 1 itteration
* less then number of Strings in array, because
* we have already used one String */
for(int j = 0; j < source.length - 1; j++){
System.out.print("for " + stringToAdd + " ");
/* if next string index is equal to array length */
if(stringToAdd >= source.length){
System.out.print("if " + stringToAdd + " ");
stringToAdd = 0;
System.out.print("ifchanged " + stringToAdd + " ");
}
if(stringToAdd == i){
stringToAdd++;
}
if(stringToAdd >= source.length){
stringToAdd = 0;
}
System.out.print("before temp " + stringToAdd + " ");
temp += source[stringToAdd];
stringToAdd++;
}
System.out.println();
nextStart++;
set.add(temp);
temp = source[i];
}
}
list.addAll(set);
return list;
}
}