Epic Systems Interview Question
System Administratorsstring sn;
string out;
string letter[10][5] =
{
{"0","0","0","0","0"},
{"0","0","0","0","0"},
{"A","B","C","0","0"},
{"D","E","F","0","0"},
{"G","H","I","0","0"},
{"J","K","L","0","0"},
{"M","N","O","0","0"},
{"P","Q","R","0","0"},
{"S","T","U","0","0"},
{"V","W","X","Y","Z"}
};
void sub(unsigned int k) {
if (k >= sn.size()) {
cout << out << endl;
return;
}
int j = int(sn[k]) - int('0');
if (j == 0 || j == 1)
sub(k + 1);
for (int i = 0; i < 5; ++i) {
if (letter[j][i] != "0") {
out = out + letter[j][i];
sub(k + 1);
out.erase(out.size() - 1, 1);
}
}
}
int main() {
int n;
cin >> n;
stringstream ss;
ss << n;
sn = ss.str();
out = "";
sub(0);
return 0;
}
// recursive solution
replace(string source,string target,int i,int n,int j)
// n = length of source string
// i = current character to be scanned from source string
// j = character to be replaced in target string
{
if(i == n+1)
target[i] = '\0';
print str;
else
{switch source[i]
case 0:
case '1':
replace(source,target,i+1,n,j+1);
break;
case '2':
target[j] = 'A';
replace(source,target,i+1,n,j+1);
target[j] = 'B';
replace(source,target,i+1,n,j+1);
target[j] = 'C';
replace(source,target,i+1,n,j+1);
break;
case '3':
target[j] = 'D';
replace(source,target,i+1,n,j+1);
target[j] = 'E';
replace(source,target,i+1,n,j+1);
target[j] = 'F';
replace(source,target,i+1,n,j+1);
break;
case '4':
target[j] = 'G';
replace(source,target,i+1,n,j+1);
target[j] = 'H';
replace(source,target,i+1,n,j+1);
target[j] = 'I';
replace(source,target,i+1,n,j+1);
break;
case '5':
target[j] = 'J';
replace(source,target,i+1,n,j+1);
target[j] = 'K';
replace(source,target,i+1,n,j+1);
target[j] = 'L';
case '6':
target[j] = 'M';
replace(source,target,i+1,n,j+1);
target[j] = 'N';
replace(source,target,i+1,n,j+1);
target[j] = 'O';
replace(source,target,i+1,n,j+1);
break;
replace(source,target,i+1,n,j+1);
break;
case '7':
target[j] = 'P';
replace(source,target,i+1,n,j+1);
target[j] = 'Q';
replace(source,target,i+1,n,j+1);
target[j] = 'R';
replace(source,target,i+1,n,j+1);
break;
case '8':
target[j] = 'S';
replace(source,target,i+1,n,j+1);
target[j] = 'T';
replace(source,target,i+1,n,j+1);
target[j] = 'U';
replace(source,target,i+1,n,j+1);
break;
case '9':
target[j] = 'V';
replace(source,target,i+1,n,j+1);
target[j] = 'W';
replace(source,target,i+1,n,j+1);
target[j] = 'X';
replace(source,target,i+1,n,j+1);
target[j] = 'Y';
replace(source,target,i+1,n,j+1);
target[j] = 'Z';
replace(source,target,i+1,n,j+1);
break;
}
}
// recursive solution
replace(string source,string target,int i,int n,int j)
// n = length of source string
// i = current character to be scanned from source string
// j = character to be replaced in target string
{
if(i == n+1)
target[i] = '\0';
print str;
else
{switch source[i]
case 0:
case '1':
replace(source,target,i+1,n,j+1);
break;
case '2':
target[j] = 'A';
replace(source,target,i+1,n,j+1);
target[j] = 'B';
replace(source,target,i+1,n,j+1);
target[j] = 'C';
replace(source,target,i+1,n,j+1);
break;
case '3':
target[j] = 'D';
replace(source,target,i+1,n,j+1);
target[j] = 'E';
replace(source,target,i+1,n,j+1);
target[j] = 'F';
replace(source,target,i+1,n,j+1);
break;
case '4':
target[j] = 'G';
replace(source,target,i+1,n,j+1);
target[j] = 'H';
replace(source,target,i+1,n,j+1);
target[j] = 'I';
replace(source,target,i+1,n,j+1);
break;
case '5':
target[j] = 'J';
replace(source,target,i+1,n,j+1);
target[j] = 'K';
replace(source,target,i+1,n,j+1);
target[j] = 'L';
case '6':
target[j] = 'M';
replace(source,target,i+1,n,j+1);
target[j] = 'N';
replace(source,target,i+1,n,j+1);
target[j] = 'O';
replace(source,target,i+1,n,j+1);
break;
replace(source,target,i+1,n,j+1);
break;
case '7':
target[j] = 'P';
replace(source,target,i+1,n,j+1);
target[j] = 'Q';
replace(source,target,i+1,n,j+1);
target[j] = 'R';
replace(source,target,i+1,n,j+1);
break;
case '8':
target[j] = 'S';
replace(source,target,i+1,n,j+1);
target[j] = 'T';
replace(source,target,i+1,n,j+1);
target[j] = 'U';
replace(source,target,i+1,n,j+1);
break;
case '9':
target[j] = 'V';
replace(source,target,i+1,n,j+1);
target[j] = 'W';
replace(source,target,i+1,n,j+1);
target[j] = 'X';
replace(source,target,i+1,n,j+1);
target[j] = 'Y';
replace(source,target,i+1,n,j+1);
target[j] = 'Z';
replace(source,target,i+1,n,j+1);
break;
}
}
<pre lang="" line="1" title="CodeMonkey72890" class="run-this">import java.util.*;
import java.io.*;
class Keypad
{
public static final int[] keypadint= {0, 1, 65, 68, 71, 74,77,80,84,87};
public static void main (String arg[])
{
System.out.println("************************");
printRecursiveAlphabet ("",430017814);
}
public static void printRecursiveAlphabet (String string,int num)
{
int len,mod=0,count=0;
String newstr;
if (num==0)
{
System.out.println(string);
return;
}
mod=num%10;
num = num/10;
while (mod==0||mod==1)
{
mod=num%10;
num = num/10;
//System.out.println("mod : "+mod+" num : "+num);
if (count==10)
break;
count++;
}
if (mod==9 || mod==7)
{
len=4;
}
else
{
len = 3;
}
for (int i=0;i<len;i++)
{
newstr = string;
string =string+ (char)(keypadint[mod]+i);
//System.out.println("String : "+string+" num : "+num);
printRecursiveAlphabet(string, num);
string = newstr;
}
}
}</pre><pre title="CodeMonkey72890" input="yes">
</pre>
import java.util.Hashtable;
public class Epic1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable<String,String> table = new Hashtable<String,String>();
table.put("2", "abc");
table.put("3", "def");
table.put("4", "ghi");
table.put("5", "jkl");
table.put("6", "mno");
table.put("7","pqr");
table.put("8", "stuv");
table.put("9", "wxyz");
String first = table.get("2");
String second = table.get("3");
String third = table.get("4");
for(int i=0;i<=first.length()-1;i++){
char firstChar = first.charAt(i);
for(int j=0;j<=second.length()-1;j++){
char secondChar = second.charAt(j);
for(int k=0;k<=third.length()-1;k++){
char thirdChar = third.charAt(k);
System.out.println(""+firstChar+""+secondChar+""+thirdChar);
}
}
}
}
}
import java.util.Hashtable;
public class Epic1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable<String,String> table = new Hashtable<String,String>();
table.put("2", "abc");
table.put("3", "def");
table.put("4", "ghi");
table.put("5", "jkl");
table.put("6", "mno");
table.put("7","pqr");
table.put("8", "stuv");
table.put("9", "wxyz");
String first = table.get("2");
String second = table.get("3");
String third = table.get("4");
for(int i=0;i<=first.length()-1;i++){
char firstChar = first.charAt(i);
for(int j=0;j<=second.length()-1;j++){
char secondChar = second.charAt(j);
for(int k=0;k<=third.length()-1;k++){
char thirdChar = third.charAt(k);
System.out.println(""+firstChar+""+secondChar+""+thirdChar);
}
}
}
}
}
public class PrintSeq {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String s = "";
printSeq(str,str.length(),s);
}
static void printSeq(String str , int index, String s){
if(index == 0){
System.out.println(s);
}else{
if(str.charAt(str.length()-index) == '0'){
--index;
printSeq(str,index,s);
}else if(str.charAt(str.length()-index) == '1'){
--index;
printSeq(str,index,s);
}else if(str.charAt(str.length()-index) == '2'){
--index;
printSeq(str,index,s + "a");
printSeq(str,index,s + "b");
printSeq(str,index,s + "c");
}else if(str.charAt(str.length()-index) == '3'){
--index;
printSeq(str,index,s + "d");
printSeq(str,index,s + "e");
printSeq(str,index,s + "f");
}else if(str.charAt(str.length()-index) == '4'){
--index;
printSeq(str,index,s + "g");
printSeq(str,index,s + "h");
printSeq(str,index,s + "i");
}else if(str.charAt(str.length()-index) == '5'){
--index;
printSeq(str,index,s + "j");
printSeq(str,index,s + "k");
printSeq(str,index,s + "l");
}else if(str.charAt(str.length()-index) == '6'){
--index;
printSeq(str,index,s + "m");
printSeq(str,index,s + "n");
printSeq(str,index,s + "o");
}else if(str.charAt(str.length()-index) == '7'){
--index;
printSeq(str,index,s + "p");
printSeq(str,index,s + "q");
printSeq(str,index,s + "r");
}else if(str.charAt(str.length()-index) == '8'){
--index;
printSeq(str,index,s + "s");
printSeq(str,index,s + "t");
printSeq(str,index,s + "u");
}else if(str.charAt(str.length()-index) == '9'){
--index;
printSeq(str,index,s + "v");
printSeq(str,index,s + "w");
printSeq(str,index,s + "x");
printSeq(str,index,s + "y");
printSeq(str,index,s + "z");
}
}
}
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include<conio.h>
char arr[10][5]={
'0','0','0','0','0',
'0','0','0','0','0',
'A','B','C','0','0',
'D','E','F','0','0',
'G','H','I','0','0',
'J','K','L','0','0',
'M','N','O','0','0',
'P','Q','R','0','0',
'S','T','U','0','0',
'V','W','X','Y','Z'
};
int count[10]={0,0,3,3,3,3,3,3,3,5};
void print(char arrm[],int index)
{
int i;
for(i=0;i<index;++i)
printf("%c",arrm[i]);
printf("\n");
}
void func(char arrm[],int n,int index,int i,char arrg[])
{
int x,y,j;
if(i==n)
print(arrm,index);
else
{
x=arrg[i]-'0';
if(x==0||x==1)
{
func(arrm,n,index,i+1,arrg);
return;
}
else
{
y=count[x];
for(j=0;j<y;++j)
{
arrm[index]=arr[x][j];
func(arrm,n,index+1,i+1,arrg);
}
}
}
}
int main() {
int l;
char arrg[100],arrn[100]={'0'};
scanf("%s",arrg);
l=strlen(arrg);
func(arrn,l,0,0,arrg);
getch();
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include<conio.h>
char arr[10][5]={
'0','0','0','0','0',
'0','0','0','0','0',
'A','B','C','0','0',
'D','E','F','0','0',
'G','H','I','0','0',
'J','K','L','0','0',
'M','N','O','0','0',
'P','Q','R','0','0',
'S','T','U','0','0',
'V','W','X','Y','Z'
};
int count[10]={0,0,3,3,3,3,3,3,3,5};
void print(char arrm[],int index)
{
int i;
for(i=0;i<index;++i)
printf("%c",arrm[i]);
printf("\n");
}
void func(char arrm[],int n,int index,int i,char arrg[])
{
int x,y,j;
if(i==n)
print(arrm,index);
else
{
x=arrg[i]-'0';
if(x==0||x==1)
{
func(arrm,n,index,i+1,arrg);
return;
}
else
{
y=count[x];
for(j=0;j<y;++j)
{
arrm[index]=arr[x][j];
func(arrm,n,index+1,i+1,arrg);
}
}
}
}
int main() {
int l;
char arrg[100],arrn[100]={'0'};
scanf("%s",arrg);
l=strlen(arrg);
func(arrn,l,0,0,arrg);
getch();
return 0;
}
Very Simple DFS based solution....
class GeneratePhoneDigits {
static char[][] letter =
{
{},{},{'A','B','C'},{'D','E','F'},{'G','H','I'},{'J','K','L'},{'M','N','O'},{'P','Q','R'},
{'S','T','U'},{'V','W','X','Y','Z'}
};
public static void generatePhoneDigits(String s, char[][] letters,int index, String temp)
{
if (index == s.length()) {System.out.println(temp); return;}
if ((s.charAt(index)-'0')==0||(s.charAt(index)-'0')==1)
generatePhoneDigits(s,letters,index+1,temp);
for (int i=0;i<letters[((int)(s.charAt(index)-'0'))].length;i++)
generatePhoneDigits(s,letters,index+1,temp+(letters[((int)(s.charAt(index)-'0'))][i]));
}
public static void main(String[] args) {
String s="27190000";
generatePhoneDigits(s,letter,0,"");
}
}
- Anonymous May 12, 2010