Sponsor

test

Sample Text

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

About & Social

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum viverra pharetra. Nulla facilisis, sapien non pharetra venenatis, tortor erat tempus est, sed accumsan odio ante ac elit. Nulla hendrerit a est vel ornare. Proin eu sapien a sapien dignissim feugiat non eget turpis. Proin at accumsan risus. Pellentesque nunc diam, congue ac lacus

My First Blog Page blog.codingninjas.in Coding Ninja first Blog ...

Search This Blog

Archive

Tags

Post Top Ad

ads

Latest Admit Cards

Beauty

Latest Admissions

Hot

Latest Syllabus

Latest Answer Key

About Us

Recent

Subscribe To Get All The Latest Updates!

email updates

Recent Posts

ads

Post Top Ad

Return Keypad Code

No comments :

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

No comments :

Post a Comment