I am trying to find out the algorithm for the following using C#: having a=1, b=2, c=3 etc to z. When given a string of numbers I need to calculate the number of combinations of alphabets. For example, for input: '123', the output would be 3 since '1'+'2'+'3'=abc, '1'+23'=aw, '12'+'3'=lc
I know there should be a recursive function to go through each number. And inside the function there should be a loop. And if the number is greater than 26 skip that combination.
Here is what I have been trying so far:
static void Main(string[] args)
{
int no_combination = getCombinations("123");
}
public int getCombinations(string str)
{
char[] numbers = str.ToCharArray();
List<string> combinatins = new List<string>();
int no_combinations = getPossibilities(numbers, 0, out combinatins);
return no_combinations;
}
public int getPossibilities(char[] numbers, int index, out List<string> combinations)
{
combinations = new List<string>();
int possibilities = 0;
string combination = "";
for (int i = index; i < numbers.Length; i++)
{
combination += numbers[i] + " ";
}
combinations.Add(combination);
possibilities = combinations.Count;
if (index>=numbers.Length)
{
return possibilities;
}
getPossibilities(numbers, ++index, out combinations);
return possibilities;
}
I know there are logical errors. The combinations list is getting reset in each call. And the way the combinations are created is missing a tweak that I can't get. I am not expecting to write the whole code. Helpful hints would be appreciated.