Return Keypad Code
Given an integer n, using phone keypad find out all the possible strings that can be made using digits of input n.
Return empty string for numbers 0 and 1.
Note : The order of strings are not important.
Input Format :
Integer n
Output Format :
All possible strings in different lines
Constraints :
1 <= n <= 10^6
Sample Input:
23
Sample Output:
ad
ae
af
bd
be
bf
cd
ce
cf
public class key {
static String[] code = {"","abc","def","ghi","jkl","mno","pqr","st","uvwx","yz"};
public static String[] combinaton(int n)
{
if(n==0)
{
String[] re = new String[1];
re[0] = "";
return re;
}
String ch = code[n%10];
String[] rr = combinaton(n/10);
String[] mr = new String[rr.length*ch.length()];
int k=0;
for(int i=0;i<rr.length;i++)
{
for(int j=0;j<ch.length();j++)
{
mr[k] = ch.charAt(j)+rr[i];
k++;
}
}
return mr;
}
In the second method we have only used Array List
public class combination {
static String codes[] = {"","abc","def","ghi","jkl","mno","pqr","st","uvwx","yz"};
public static ArrayList<String> com(String str)
{
if(str.length()==0)
{
ArrayList<String> ans = new ArrayList<>();
ans.add("");
return ans;
}
char ch = str.charAt(0);
ArrayList<String> rr = com(str.substring(1));
ArrayList<String> my = new ArrayList<>();
for(String i:rr)
{
String code = codes[ch-'0'];
for(int j=0;j<code.length();j++)
{
char c = code.charAt(j);
my.add(c+i);
}
}
return my;
}
Subscribe to:
Post Comments
(
Atom
)



No comments :
Post a Comment