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());
        }
    }
}

}

0 Thoughts on “String Decode Problem

  1. Karthik Nedunchezhiyan on January 23, 2017 at 5:35 pm said:

    It can be simply write in C like this:->
    #include
    int main()
    {
    char word[]=”1234″;
    int i;
    for(i=0;i<strlen(word);i++)
    {
    char temp= (char)((int)word[i]+49-1);
    printf("%c",temp);
    }
    return 0;
    }

  2. Arbrim on March 30, 2017 at 10:06 pm said:

    //JAVA

    public class TrickyQuestions {
    public static void main(String[] args) {
    System.out.println(numberToString(“09343″));
    }

    public static String numberToString(String numrat)
    {
    String shkronjat = “abcdefghij”;
    String kthim = “”;

    for (int i = 0; i < numrat.length(); i++){
    kthim += shkronjat.toCharArray()[Character.getNumericValue(numrat.charAt(i))];
    }
    return kthim;
    }
    }

  3. nimitha on May 9, 2018 at 3:57 pm said:

    good morning

  4. HIMANSHU SEN on December 25, 2018 at 12:33 pm said:

    public static void main(String[] args) {
    String s = “1123″;
    System.out.println(gen(s, “”));
    }

    static int gen(String s, String perm) {
    if (s.isEmpty()) {
    System.out.println(perm);
    return 1;
    }
    int c = 0;
    c += gen(s.substring(1), perm + code(Integer.parseInt(s.substring(0, 1))));
    if (s.length() > 1) {
    c += gen(s.substring(2), perm + code(Integer.parseInt(s.substring(0, 2))));
    }
    return c;
    }

    static char code(int num) {
    return (char) (‘a’ + num – 1);
    }

  5. Sangeetha on May 19, 2020 at 4:10 am said:

    It is very simple in while using python
    A=input(“enter your number:”);
    B=['a','b','c','d',e','f,'g','h','j','k','l','m','n','o','p','q',r','s','t','u','v,''w','x','y,''z']
    A=b[A];
    Print (A);

Leave a Reply to Karthik Nedunchezhiyan Cancel 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