Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package phonepadproblem;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- /**
- *
- * @author Green
- */
- public class PhonePadProblem {
- public static Map<Integer, char[]> numbersMap = new HashMap<Integer, char[]>();
- public static ArrayList<String> solutions = new ArrayList<String>();
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- numbersMap.put(0,new char[]{'1', '2', '3'} );
- numbersMap.put(1,new char[]{'4', '5', '6'} );
- numbersMap.put(2,new char[]{'a', 'b', 'c'} );
- numbersMap.put(3,new char[]{'d', 'e', 'f'} );
- numbersMap.put(4,new char[]{'g', 'h', 'i'} );
- numbersMap.put(5,new char[]{'j', 'k', 'l'} );
- numbersMap.put(6,new char[]{'m', 'n', 'o'} );
- numbersMap.put(7,new char[]{'p', 'q', 'r', 's'} );
- numbersMap.put(8,new char[]{'t', 'u', 'v'} );
- numbersMap.put(9,new char[]{'w', 'x', 'y', 'z'} );
- Scanner sc = new Scanner(System.in);
- String number = sc.nextLine();
- getSolutions(number, "");
- System.out.println(solutions);
- }
- public static void getSolutions(String number, String solution) {
- if(number.length() == 0) {
- solutions.add(solution);
- return;
- }
- char possibleLetters[];
- try{
- possibleLetters = numbersMap.get(Integer.parseInt(number.substring(0, 1)));
- } catch (StringIndexOutOfBoundsException ex) {
- possibleLetters = numbersMap.get(Integer.parseInt(number));
- }
- for(char c: possibleLetters) {
- if(number.length() == 1) {
- getSolutions("", solution+c);
- } else {
- getSolutions(number.substring(1), solution+c);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement