I found a link online that shows an algorithm to generate all combinations of a string: http://www.mytechinterviews.com/combinations-of-a-string
Algorithm is copied belo
It balances the first line of the loop body, restoring outstr to what it was at the top of the loop body (by removing the character from instr that was appended).
Simplest way of calculating the possible combinations of strings is here ...
Mathematically to find R combinations in a given lot of N = NcR
So what we are finding here is, all possible combinations = Nc0 + Nc1 .... + Ncn = 2 Pow N
So you get 2 Pow N combinations for given word of length N characters.
If you represent 1 to (2 Pow N) integers in binary, and place your char in the place where 1 is present, finally you would get the solution.
Input : ABC
Solution :
ABC length is 3. So possible combinations 2 Pow 3 = 8
If 0 - 8 represented in binary
000 =
001 = C
010 = B
011 = BC
100 = A
101 = AC
110 = AB
111 = ABC
all possible combinations are shown above.
import com.google.common.collect.Lists;
import java.util.List;
public class Combinations {
public static String[] getCombinations(final String input) {
final List<String> combinations = Lists.newArrayList();
getCombinations(input.toCharArray(), combinations, 0, "");
return combinations.toArray(new String[0]);
}
private static void getCombinations(final char[] input, final List<String> combinations, final int index, final String combination) {
if (index == input.length) {
combinations.add(combination);
return;
}
getCombinations(input, combinations, index + 1, combination + String.valueOf(input[index]));
getCombinations(input, combinations, index + 1, combination);
}
}
Corresponding tests:
import org.hamcrest.Matchers;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
public class CombinationsTest {
@Test
public void testCombinations() {
verify(Combinations.getCombinations(""), "");
verify(Combinations.getCombinations("a"), "a", "");
verify(Combinations.getCombinations("ab"), "ab", "a", "b", "");
verify(Combinations.getCombinations("abc"), "abc", "ab", "ac", "a", "bc", "b", "c", "");
verify(Combinations.getCombinations("abcd"),
"abcd", "abc", "abd", "ab", "acd", "ac", "ad", "a", "bcd", "bc", "bd", "b", "cd", "c", "d", "");
}
private void verify(final String[] actual, final String... expected) {
assertThat(actual, Matchers.equalTo(expected));
}
}
// IF YOU NEED REPEATITION USE ARRAYLIST INSTEAD OF SET!!
import java.util.*;
public class Permutation {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("ENTER A STRING");
Set<String> se=find(in.nextLine());
System.out.println((se));
}
public static Set<String> find(String s)
{
Set<String> ss=new HashSet<String>();
if(s==null)
{
return null;
}
if(s.length()==0)
{
ss.add("");
}
else
{
char c=s.charAt(0);
String st=s.substring(1);
Set<String> qq=find(st);
for(String str:qq)
{
for(int i=0;i<=str.length();i++)
{
ss.add(comb(str,c,i));
}
}
}
return ss;
}
public static String comb(String s,char c,int i)
{
String start=s.substring(0,i);
String end=s.substring(i);
return start+c+end;
}
}
// IF YOU NEED REPEATITION USE ARRAYLIST INSTEAD OF SET!!
Below code is to generate permutation and combination of string, basically the concept is to pick one character at a time:
public class permutecombo
{
static void initiate(String s)
{
permute("", s);
System.out.println("----------------------------------------- ");
combo("", s);
System.out.println("----------------------------------------- ");
}
static void combo(String prefix, String s)
{
int N = s.length();
System.out.println(prefix);
for (int i = 0 ; i < N ; i++)
combo(prefix + s.charAt(i), s.substring(i+1));
}
static void permute(String prefix, String s)
{
int N = s.length();
if (N == 0)
System.out.println(" " + prefix);
for (int i = 0 ; i < N ; i++)
permute(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N));
}
public static void main(String[] args)
{
String s = "1234";
initiate(s);
}
}
The call of outstr.deleteCharAt
counters the effect of outstr.append
by deleting the last character of the outstr
.
Each loop iteration proceeds as follows:
i+1