1

I'm trying to write an algorithm that will take a group of letters and a int containing the size of the word and create every possible grouping of letters for this, but I'm totally stuck...

Example:

**Input**
Letters: E
Size: 4

**Output** 
E___
E_E_
_E_E
EE__
_EE_
___E

etc..

Another example

**Input**
Letters: E, A
Size: 4

**Output** 
EA__
AE__
E_A_
_E_A
A_E_
etc...
  • See this: http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n The problem can be reduced to listing all n choose k combinations – nhahtdh Aug 12 '12 at 15:49

1 Answers1

0

Here is an algorithm which which uses recursion to solve the problem: Define a method which takes the length, char array (including '_' ) and another String array as arguments.

String[] meth(int l;char ch[],String[] s)
Define a new String array (ns) which contains N elements where N=length of array s * l
Copy each element of s into ns thrice, each time appending an element of ch
Make a recursive call to meth with (l, ch, ns) if the length of ns is less than l


If the input is {'E', 'A'} and 4
call meth(4,new char[]{'E','A','_'},new String[]{} )
Ranjith
  • 1,623
  • 3
  • 21
  • 34