We can divide problem statement into two parts.
- Extracting combination of three characters out of five.
- Combination of three characters that are being extracted.
For first part we would declare an array that has possible combination of three character out of five. With the extracted three character we will make different combinations.
Note: You can add the combination in arrIndexes if I missed any.
string word = "hello";
int [,] arrIndexes = new int[9,3] {{0,1,2}, {0,1,3}, {0,1,4}, {0,2,3}, {0,2,4}, {0,3,4}, {1,2,3}, {1,3,4}, {2,3,4}};
for(int i=0; i < 9; i++)
{
string sub = "";
for(int j=0; j<3; j++)
sub += word[arrIndexes[i,j]];
Console.Write("{0}{1}{2}",sub[0], sub[1], sub[2]);
Console.Write("\t{0}{1}{2}",sub[2], sub[1], sub[0]);
Console.Write("\t{0}{1}{2}",sub[1], sub[0],sub[2]);
Console.Write("\t{0}{1}{2}",sub[0], sub[2], sub[1]);
Console.Write("\t{0}{1}{2}",sub[1], sub[2], sub[0]);
Console.WriteLine("\t{0}{1}{2}",sub[2], sub[0], sub[1]);
}
As pointed by @Rawling we can further generalize it to get the indexes for the character for getting three character word.
string word = "hello";
for(int i=0; i<word.Length-2; i++)
for(int j=i+1; j< word.Length-1; j++)
for(int k=j+1; k < word.Length; k++)
{
string sub = string.Format("{0}{1}{2}",word[i], word[j], word[k]);
Console.Write("{0}{1}{2}",sub[0], sub[1], sub[2]);
Console.Write("\t{0}{1}{2}",sub[2], sub[1], sub[0]);
Console.Write("\t{0}{1}{2}",sub[1], sub[0],sub[2]);
Console.Write("\t{0}{1}{2}",sub[0], sub[2], sub[1]);
Console.Write("\t{0}{1}{2}",sub[1], sub[2], sub[0]);
Console.WriteLine("\t{0}{1}{2}",sub[2], sub[0], sub[1]);
}
Output
hel leh ehl hle elh lhe
hel leh ehl hle elh lhe
heo oeh eho hoe eoh ohe
hll llh lhl hll llh lhl
hlo olh lho hol loh ohl
hlo olh lho hol loh ohl
ell lle lel ell lle lel
elo ole leo eol loe oel
elo ole leo eol loe oel
llo oll llo lol lol oll
Edit
You can further generalize it for getting the combinations of three letter extracted word
string word = "hello";
for(int i=0; i<word.Length-2; i++)
for(int j=i+1; j< word.Length-1; j++)
for(int k=j+1; k < word.Length; k++)
{
string sub = string.Format("{0}{1}{2}",word[i], word[j], word[k]);
for(int l=0; l<3;l++)
for(int m=0; m<3;m++)
for(int n=0; n<3;n++)
if(l != m && m != n && l!=n)
Console.Write("\t{0}{1}{2}",sub[l], sub[m], sub[n]);
Console.WriteLine("");
}
Output
hel hle ehl elh lhe leh
hel hle ehl elh lhe leh
heo hoe eho eoh ohe oeh
hll hll lhl llh lhl llh
hlo hol lho loh ohl olh
hlo hol lho loh ohl olh
ell ell lel lle lel lle
elo eol leo loe oel ole
elo eol leo loe oel ole
llo lol llo lol oll oll