• Post Reply Bookmark Topic Watch Topic
  • New Topic

all combination of string  RSS feed

 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Your subject says one, but your example has the other.
 
Anand Narayan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!