Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mix multiple strings into all the possible combinations

I have some strings.

  1. 1

  2. 2

  3. 3

How do I combine them into all their unique combinations?

  1. 123

  2. 132

  3. 213

  4. 231

  5. 312

  6. 321

Here is the code I have, but I would like to work without the Random class because I understand that this is not the best way to do it.

import java.util.Random;

public class Solution
{
    public static void main(String[] args)
    {
        String[] names = new String[]{"string1", "string2", "string3"};
        for (int i = 0; i < 9; i++) {
            Random rand = new Random();
            int rand1 = rand.nextInt(3);
            System.out.println(names[rand.nextInt(3)] +
                    names[rand1] +
                    names[rand.nextInt(3)]);
        }
    }
}
like image 224
Karkool Avatar asked Nov 29 '25 07:11

Karkool


1 Answers

You can loop over the array by creating another nested loop for each repetition.

for (String word1 : words) {
    for (String word2 : words) {
        for (String word3 : words) {
            System.out.println(word1 + word2 + word3);
        }
    }
}

Here is how to avoid having the same word in one combination.

for (String word1 : words) {
    for (String word2 : words) {
        if ( !word1.equals(word2)) {
            for (String word3 : words) {
                if ( !word3.equals(word2) && !word3.equals(word1)) {
                    System.out.println(word1 + word2 + word3);
                }
            }
        }
    }
}

Here is a class version that is capable of multiple lengths, using backtracking.

import java.util.ArrayList;
import java.util.List;


public class PrintAllCombinations {

    public void printAllCombinations() {
        for (String combination : allCombinations(new String[] { "A", "B", "C" })) {
            System.out.println(combination);
        }
    }

    private List<String> allCombinations(final String[] values) {
        return allCombinationsRecursive(values, 0, values.length - 1);
    }

    private List<String> allCombinationsRecursive(String[] values, final int i, final int n) {
        List<String> result = new ArrayList<String>();
        if (i == n) {
            StringBuilder combinedString = new StringBuilder();
            for (String value : values) {
                combinedString.append(value);
            }
            result.add(combinedString.toString());
        }
        for (int j = i; j <= n; j++) {
            values = swap(values, i, j);
            result.addAll(allCombinationsRecursive(values, i + 1, n));
            values = swap(values, i, j); // backtrack
        }
        return result;
    }

    private String[] swap(final String[] values, final int i, final int j) {
        String tmp = values[i];
        values[i] = values[j];
        values[j] = tmp;
        return values;
    }

}

Please note that using the random method, it is never guaranteed that all combinations are being get. Therefore, it should always loop over all values.

like image 151
Jordi Betting Avatar answered Dec 01 '25 21:12

Jordi Betting