Reputation: 25058
I have a string
to do so I am doing:
string[] combinations = Combinations("LEQN");
foreach (string s in combinations)
{
Console.WriteLine(s);
}
and method Combinations:
public static string[] Combinations(string str)
{
if (string.IsNullOrEmpty(str)) throw new ArgumentException("Invalid input");
if (str.Length == 1) return new string[] { str };
// read the last character
char c = str[str.Length - 1];
// apart from the last character send remaining string for further processing
string[] returnArray = Combinations(str.Substring(0, str.Length - 1));
// List to keep final string combinations
List<string> finalArray = new List<string>();
// add whatever is coming from the previous routine
foreach (string s in returnArray)
finalArray.Add(s);
// take the last character
finalArray.Add(c.ToString());
// take the combination between the last char and the returning strings from the previous routine
foreach (string s in returnArray)
finalArray.Add(s + c);
return finalArray.ToArray();
}
this would print:
L
E
LE
Q
LQ
EQ
LEQ
N
LN
EN
LEN
QN
LQN
EQN
LEQN
which is correct, however I woul like to get result like
L N Q E LN NQ EL QE LNQ ELN QEL NQE NQEL
How to accomplish that?
Upvotes: 0
Views: 243