Open rocksc30 opened 1 year ago
class Solution { Stack<Character> path = new Stack<>(); List<String> ans = new ArrayList<>(); String[] number = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; public List<String> letterCombinations(String digits) { if("".equals(digits)) return ans; helper(0, digits); return ans; } void helper(int n, String digits){ if (n == digits.length()){ ArrayList<Character> characters = new ArrayList<>(path); StringBuilder sb = new StringBuilder(); characters.forEach(item -> sb.append(item)); ans.add(sb.toString()); return; } char c = digits.charAt(n); String cur = number[c - '0']; for (int i = 0; i < cur.length(); i++) { path.push(cur.charAt(i)); helper(n + 1, digits); path.pop(); } } }