|
| 1 | +✅ Java Code (Backtracking Approach) |
| 2 | + |
| 3 | +class Solution { |
| 4 | + private static final String[] KEYPAD = { |
| 5 | + "", "", "abc", "def", "ghi", |
| 6 | + "jkl", "mno", "pqrs", "tuv", "wxyz" |
| 7 | + }; |
| 8 | + |
| 9 | + public List<String> letterCombinations(String digits) { |
| 10 | + List<String> result = new ArrayList<>(); |
| 11 | + if (digits.isEmpty()) return result; |
| 12 | + backtrack(digits, 0, new StringBuilder(), result); |
| 13 | + return result; |
| 14 | + } |
| 15 | + |
| 16 | + private void backtrack(String digits, int index, StringBuilder current, List<String> result) { |
| 17 | + if (index == digits.length()) { |
| 18 | + result.add(current.toString()); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + String letters = KEYPAD[digits.charAt(index) - '0']; |
| 23 | + for (char c : letters.toCharArray()) { |
| 24 | + current.append(c); |
| 25 | + backtrack(digits, index + 1, current, result); |
| 26 | + current.deleteCharAt(current.length() - 1); // Backtrack step |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +📘 Hinglish Explanation (Line-by-Line) |
| 32 | + |
| 33 | +private static final String[] KEYPAD = {...}; |
| 34 | +🔹 Mobile keypad ke letters ko array me store kiya (2 = abc, 3 = def ...). |
| 35 | + |
| 36 | +public List<String> letterCombinations(String digits) { |
| 37 | + List<String> result = new ArrayList<>(); |
| 38 | + 🔹 Final result store karne ke liye list banayi. |
| 39 | + |
| 40 | + if (digits.isEmpty()) return result; |
| 41 | + 🔹 Agar input empty ho toh khali list return kar do. |
| 42 | + |
| 43 | + backtrack(digits, 0, new StringBuilder(), result); |
| 44 | + 🔹 Backtracking start kiya from index 0. |
| 45 | +} |
| 46 | + |
| 47 | +private void backtrack(String digits, int index, StringBuilder current, List<String> result) { |
| 48 | + if (index == digits.length()) { |
| 49 | + result.add(current.toString()); |
| 50 | + return; |
| 51 | + 🔹 Base case: agar sab digits cover ho gaye, toh combination result me daal do. |
| 52 | + } |
| 53 | + |
| 54 | + String letters = KEYPAD[digits.charAt(index) - '0']; |
| 55 | + 🔹 Current digit ke corresponding letters uthaye (eg. '2' = "abc"). |
| 56 | + |
| 57 | + for (char c : letters.toCharArray()) { |
| 58 | + current.append(c); |
| 59 | + 🔹 Letter ko current combination me add kiya. |
| 60 | + |
| 61 | + backtrack(digits, index + 1, current, result); |
| 62 | + 🔹 Next digit ke liye recursive call. |
| 63 | + |
| 64 | + current.deleteCharAt(current.length() - 1); |
| 65 | + 🔹 Backtrack → last added letter ko hata diya. |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +🧠 Example Input: |
| 71 | + |
| 72 | +Input: digits = "23" |
| 73 | +Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] |
| 74 | +🚀 Time & Space Complexity: |
| 75 | + |
| 76 | +Time: O(3ⁿ × 4ᵐ), where n = number of digits like 2-6-8 (3 letters), m = number of digits like 7 or 9 (4 letters) |
| 77 | + |
| 78 | +Space: O(n) → recursive call stack |
| 79 | + |
| 80 | +🔗 Need more help or Java tricks? |
| 81 | +https://www.linkedin.com/in/saurabh884095/ |
0 commit comments