fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. char [][] convert = {
  13. {}, // 0
  14. {'A','B','C'}, // 1
  15. {'D','E','F'}, // 2
  16. {'G','H','I'}, // 3
  17. {'J','K','L'}, // 4
  18. {'M','N','O'}, // 5
  19. {'P','R','S'}, // 6
  20. {'T','U','V'}, // 7
  21. {'W','X','Y'} // 8
  22. };
  23.  
  24. // Sequence of keys to be pressed. In this case the pressed keys are
  25. // [ABC], [DEF], [GHI]
  26. int[] sequence = new int[] {1, 2, 3};
  27.  
  28. // List for storing the results of each level.
  29. List<List<String>> results = new ArrayList<>();
  30.  
  31. for(int i=0; i<sequence.length; i++){
  32.  
  33. // Results of this level
  34. List<String> words = new ArrayList<>();
  35. results.add(words);
  36.  
  37. List<String> prevLevelWords;
  38. if(i==0){
  39. prevLevelWords = Collections.singletonList("");
  40. } else {
  41. prevLevelWords = results.get(i-1);
  42. }
  43.  
  44. char[] thisLevelChars = convert[sequence[i]];
  45.  
  46. if(thisLevelChars.length == 0){
  47. words.addAll(prevLevelWords);
  48. } else {
  49. for(String word : prevLevelWords){
  50. for(char ch : convert[sequence[i]]){
  51. words.add(word + ch);
  52. }
  53. }
  54. }
  55. }
  56.  
  57. List<String> finalResult = results.get(sequence.length-1);
  58.  
  59. for(String word : finalResult) {
  60. System.out.println(word);
  61. }
  62.  
  63. }
  64. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
ADG
ADH
ADI
AEG
AEH
AEI
AFG
AFH
AFI
BDG
BDH
BDI
BEG
BEH
BEI
BFG
BFH
BFI
CDG
CDH
CDI
CEG
CEH
CEI
CFG
CFH
CFI