programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# all combination of string

Puneet N Vyas
Ranch Hand
Posts: 61
consider an string array of items:
String l1[]={a,b,c,d,e,f},how can we generate all combination of this array which contains output as follows:c2[]={a a,a b,a c ,a d, a e,a f,aa,ab,ac,ad,ae,af,ba,bb,bc,bd,be,bf,...........}

can any one suggest logic of doing this in java

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
First, you need to decide if you want PERMUTATIONS or COMBINATIONS. Combinations don't care about order - so "a,b" is the same as "b,a". Permutations, the order DOES matter.

Anand Narayan
Greenhorn
Posts: 2
Hi,
Hope this should work !!!

public class Assign {
public static void main(String args[])
{
String arr[]={"a","b","c","d","e","f"};

for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
System.out.println(arr[i]+","+arr[j]);
}
}

}

Regards,
aajan

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
That won't work. that will give you every permutation of every letter, but only ones that are two characters long. The original post at LEAST also needed one-character strings.

Also, if the input string had "abcdeabc", generally with combinations/permutations, you don't want duplicates.

Finally, please remember we try to not just post a solution. That doesn't help the original person figure out how to do it themselves - it just hands them the answer.

 Don't get me started about those stupid light bulbs.