class Solution {
private ArrayList<String> res;
private Map<Character, String> digitToChar = Map.of(
'2', "abc",
'3', "def",
'4', "ghi",
'5', "jkl",
'6', "mno",
'7', "pqrs",
'8', "tuv",
'9', "wxyz"
);
public List<String> letterCombinations(String digits) {
res = new ArrayList();
if (!digits.isEmpty()){
backtracking(0, "", digits);
}
return res;
}
private void backtracking(int i, String currStr, String digits) {
if (digits.length() == currStr.length()) {
res.add(currStr);
return;
}
String digit = digitToChar.get(digits.charAt(i));
for (char c : digit.toCharArray()) {
backtracking(i + 1, currStr + c, digits);
}
}
}