Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package phonepadproblem;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Scanner;
  12.  
  13. /**
  14.  *
  15.  * @author Green
  16.  */
  17. public class PhonePadProblem {
  18.     public static Map<Integer, char[]> numbersMap = new HashMap<Integer, char[]>();
  19.     public static ArrayList<String> solutions = new ArrayList<String>();
  20.     /**
  21.      * @param args the command line arguments
  22.      */
  23.     public static void main(String[] args) {
  24.         // TODO code application logic here
  25.        numbersMap.put(0,new char[]{'1', '2', '3'} );
  26.        numbersMap.put(1,new char[]{'4', '5', '6'} );
  27.        numbersMap.put(2,new char[]{'a', 'b', 'c'} );
  28.        numbersMap.put(3,new char[]{'d', 'e', 'f'} );
  29.        numbersMap.put(4,new char[]{'g', 'h', 'i'} );
  30.        numbersMap.put(5,new char[]{'j', 'k', 'l'} );
  31.        numbersMap.put(6,new char[]{'m', 'n', 'o'} );
  32.        numbersMap.put(7,new char[]{'p', 'q', 'r', 's'} );
  33.        numbersMap.put(8,new char[]{'t', 'u', 'v'} );
  34.        numbersMap.put(9,new char[]{'w', 'x', 'y', 'z'} );
  35.        Scanner sc = new Scanner(System.in);
  36.        String number = sc.nextLine();
  37.        getSolutions(number, "");
  38.        System.out.println(solutions);
  39.     }
  40.    
  41.     public static void getSolutions(String number, String solution) {
  42.         if(number.length() == 0) {
  43.             solutions.add(solution);
  44.             return;
  45.         }
  46.         char possibleLetters[];
  47.         try{
  48.             possibleLetters = numbersMap.get(Integer.parseInt(number.substring(0, 1)));
  49.         } catch (StringIndexOutOfBoundsException ex) {
  50.             possibleLetters = numbersMap.get(Integer.parseInt(number));
  51.         }
  52.         for(char c: possibleLetters) {
  53.             if(number.length() == 1) {
  54.                 getSolutions("", solution+c);
  55.             } else {
  56.                 getSolutions(number.substring(1), solution+c);
  57.             }
  58.         }
  59.     }
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement