package com.cool.hot100.backtracking; import java.util.*; /** * Created with IntelliJ IDEA. * * @Author: Cool * @Date: 2024/09/10/11:06 * DayNumber 1 * Hard 2 * Level ? */ public class Num17 { List res = new ArrayList<>(); char[] chars; public List letterCombinations(String digits) { if ("".equals(digits)) { return new ArrayList<>(); } chars = new char[digits.length()]; dfs(0, digits); return new ArrayList<>(res); } private void dfs(int index, String digits) { if (index >= digits.length()) { res.add(new String(chars)); return; } String str = getLetter(digits.charAt(index)-'0'); for (int i = 0; i < str.length(); i++) { chars[index] = str.charAt(i); dfs(index + 1, digits); } } private String getLetter(int num) { switch (num) { case 2: return "abc"; case 3: return "def"; case 4: return "ghi"; case 5: return "jkl"; case 6: return "mno"; case 7: return "pqrs"; case 8: return "tuv"; case 9: return "wxyz"; default: return ""; } } }