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. System.out.println(letterCombinations("23"));
  13. }
  14.  
  15. private static final String[] KEYS = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
  16.  
  17. public static List<String> letterCombinations(String digits) {
  18. List<String> ret = new LinkedList<String>();
  19. combination("", digits, 0, ret);
  20. return ret;
  21. }
  22.  
  23. private static void combination(String prefix, String digits, int offset, List<String> ret) {
  24. if (offset >= digits.length()) {
  25. ret.add(prefix);
  26. return;
  27. }
  28. String letters = KEYS[(digits.charAt(offset) - '0')];
  29. for (int i = 0; i < letters.length(); i++) {
  30. combination(prefix + letters.charAt(i), digits, offset + 1, ret);
  31. }
  32. }
  33. }
Success #stdin #stdout 0.07s 2184192KB
stdin
Standard input is empty
stdout
[ad, ae, af, bd, be, bf, cd, ce, cf]