fork download
  1. import java.util.ArrayList;
  2.  
  3. public class GFG {
  4.  
  5. // String array to store keypad characters
  6. static final String codes[]
  7. = { " ", "abc", "def",
  8. "ghi", "jkl", "mno",
  9. "pqr", "stu", "vwx",
  10. "yz" };
  11.  
  12. // Function that returns an Arraylist
  13. // which contains all the generated words
  14. public static ArrayList<String> printKeyWords(String str)
  15. {
  16.  
  17. // If str is empty
  18. if (str.length() == 0) {
  19. ArrayList<String> baseRes = new ArrayList<>();
  20. baseRes.add("");
  21.  
  22. // Return an Arraylist containing
  23. // empty string
  24. return baseRes;
  25. }
  26.  
  27. // First character of str
  28. char ch = str.charAt(0);
  29.  
  30. // Rest of the characters of str
  31. String restStr = str.substring(1);
  32.  
  33. ArrayList<String> prevRes = printKeyWords(restStr);
  34. ArrayList<String> Res = new ArrayList<>();
  35.  
  36. String code = codes[ch - '0'];
  37.  
  38. for (String val : prevRes) {
  39.  
  40. for (int i = 0; i < code.length(); i++) {
  41. Res.add(code.charAt(i) + val);
  42. }
  43. }
  44. return Res;
  45. }
  46.  
  47. // Driver code
  48. public static void main(String[] args)
  49. {
  50. String str = "23";
  51.  
  52. // Print all the possible words
  53. System.out.println(printKeyWords(str));
  54. }
  55. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:1: error: ‘import’ does not name a type; did you mean ‘short’?
 import java.util.ArrayList;
 ^~~~~~
 short
prog.cpp:3:1: error: expected unqualified-id before ‘public’
 public class GFG {
 ^~~~~~
stdout
Standard output is empty