- #1
kantrcr
- 1
- 0
I have a programming (Java) lab asking for a function of the prototype:
Vector phoneNumbers(String suffix, String prefix)
And I'm suppose to use recursion to fill the final returned Vector with every possible word letter combonation the digits contained in suffix could be. Prefix is a string I'm suppose to place before each combonation generated of suffix, and the concatenation of these two should go into a vector.
I've written a solution that uses a much different prototype, but I cannot figure out how I can write a recursive solution given this required prototype.
I can think of a way to do this in a language like C or C++ where i could just have a static Vector, but Java doesn't have this so I can't think of a way to keep a previous copy of my Vector after each recursive call.
Vector phoneNumbers(String suffix, String prefix)
And I'm suppose to use recursion to fill the final returned Vector with every possible word letter combonation the digits contained in suffix could be. Prefix is a string I'm suppose to place before each combonation generated of suffix, and the concatenation of these two should go into a vector.
I've written a solution that uses a much different prototype, but I cannot figure out how I can write a recursive solution given this required prototype.
Code:
public static void ListMnemonics(String suffix, String prefix, String mnem){
if(mnem.length() == suffix.length())
{
System.out.println(prefix + mnem);
return;
}
String letters = GetLettersCorrespondingToNumber((int)suffix.charAt(mnem.length()) - (int)'0');
for(int i=0; i<letters.length(); i++)
{
ListMnemonics(suffix, prefix, mnem + letters.charAt(i));
}
}public static String GetLettersCorrespondingToNumber (int num){
char [][]arr=new char[10][3];
arr[0][0]='a';
arr[0][1]='b';
arr[0][2]='c';
arr[1][0]='d';
arr[1][1]='e';
arr[1][2]='f';
arr[2][0]='g';
arr[2][1]='h';
arr[2][2]='i';
arr[3][0]='g';
arr[3][1]='h';
arr[3][2]='i';
arr[4][0]='j';
arr[4][1]='k';
arr[4][2]='l';
arr[5][0]='m';
arr[5][1]='n';
arr[5][2]='o';
arr[6][0]='p';
arr[6][1]='q';
arr[6][2]='r';
arr[7][0]='s';
arr[7][1]='t';
arr[7][2]='u';
arr[8][0]='v';
arr[8][1]='w';
arr[8][2]='x';
arr[9][0]='y';
arr[9][1]='z';
arr[9][2]='+';
String str=new String(""+arr[num][0]+arr[num][1]+arr[num][2]);
return str;
}
I can think of a way to do this in a language like C or C++ where i could just have a static Vector, but Java doesn't have this so I can't think of a way to keep a previous copy of my Vector after each recursive call.
Last edited: