Output : Combinations of Strings

for eg: A,B,C(input)

then Output :

A

B

C

AB

AC

AC

AD

BC

BD

CD

ABC

mathematical formula : nCr = n ! / (n-r) ! r !

Algorithm with explanations:

1) Enter the number of strings.

Let say 3. A,B and C.

2) now loop from i=1 to 2^3-1 = 7 (in general (2^n) -1 ).

3) Observe binary patterns for each of the numbers coming in that loop.

like in the following case.

Number Bianry Pattern

(A) (B) (C)

1 0 0 1

2 0 1 0

3 0 1 1

4 1 0 0

5 1 0 1

6 1 1 0

7 1 1 1

4) now put the appropriate string where 1 appears, and put nothing in case of 0.

ie.

1st entry becomes C

2nd entry B

3 BC

4 A

5 AC

6 AB

7 ABC

Thats all u need.

Thanks

Hey dude nice post.. Keep it up..

ReplyDelete