Letter Combinations of a Phone Number LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Problems C++
Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for the Letter Combinations of a Phone Number in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
About LeetCode
LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.
LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.
LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either Easy, Medium, or Hard.
LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:
- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.
Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.
Link for the Problem – Letter Combinations of a Phone Number– LeetCode Problem
Letter Combinations of a Phone Number– LeetCode Problem
Problem:
Given a string containing digits from 2-9
inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
![Letter Combinations of a Phone Number LeetCode Programming Solutions | LeetCode Problem Solutions in C++, Java, & Python [💯Correct] 2 200px Telephone keypad2.svg](https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
Example 1:
Input: digits = "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Example 2:
Input: digits = "" Output: []
Example 3:
Input: digits = "2" Output: ["a","b","c"]
Constraints:
0 <= digits.length <= 4
digits[i]
is a digit in the range['2', '9']
.
Letter Combinations of a Phone Number– LeetCode Solutions
class Solution { public: vector<string> letterCombinations(string digits) { if (digits.empty()) return {}; vector<string> ans; dfs(digits, 0, "", ans); return ans; } private: const vector<string> digitToLetters{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; void dfs(const string& digits, int i, string&& path, vector<string>& ans) { if (i == digits.length()) { ans.push_back(path); return; } for (const char letter : digitToLetters[digits[i] - '0']) { path.push_back(letter); dfs(digits, i + 1, move(path), ans); path.pop_back(); } } };
class Solution { public List<String> letterCombinations(String digits) { if (digits.isEmpty()) return new ArrayList<>(); List<String> ans = new ArrayList<>(); ans.add(""); final String[] digitToLetters = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; for (final char d : digits.toCharArray()) { List<String> temp = new ArrayList<>(); for (final String s : ans) for (final char c : digitToLetters[d - '0'].toCharArray()) temp.add(s + c); ans = temp; } return ans; } }
class Solution: def letterCombinations(self, digits: str) -> List[str]: if not digits: return [] ans = [''] digitToLetters = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'] for d in digits: temp = [] for s in ans: for c in digitToLetters[ord(d) - ord('0')]: temp.append(s + c) ans = temp return