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