String Decode Problem

Problem:

If a=1, b=2, c=3,….z=26. Given a string, find all possible codes that string
can generate. Give a count as well as print the strings.

For example:
Input: “1123″. You need to general all valid alphabet codes from this string.

Output List
aabc //a = 1, a = 1, b = 2, c = 3
kbc // since k is 11, b = 2, c= 3
alc // a = 1, l = 12, c = 3
aaw // a= 1, a =1, w= 23
kw // k = 11, w = 23

Solution:



class Crazyforcode
{
	
static void Main(string[] args)
{
    if (args.Length != 1)
    {
        Console.WriteLine("Usage: AllPossibleCodes.exe <number>");
        return;
    }
    Process(args[0], "");
}


Process(args[0], "");

static void ProcessString(string number,string code)
{
    if (number == null ||
        number.Length == 0)
    {
        if (code != null && code.Length > 0)
        {
            Console.WriteLine(code);
        }
        return;
    }
    ProcessString(number.Substring(1), code + 
    	(Convert.ToChar('a' + (number[0] - '0') - 1)).ToString());
    if (number.Length > 1)
    {
        int n = Convert.ToInt32(number.Substring(0, 2));
        if (n <= 26)
        {
            ProcessString(number.Substring(2), code + 
            	(Convert.ToChar('a' + n - 1)).ToString());
        }
    }
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation