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.
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'].
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Here is the solution. Kindly suggest me if you have a better solution.
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
We are going to use vectors from C++.
What is a vector?
In C++, a vector is a dynamic array data structure that is part of the Standard Template Library (STL). It provides an easy way to store and manage sequences of elements, including elements of any data type (e.g. integers, strings, etc.). Some of the benefits of using vector include:
- Dynamic size: The size of a vector can be changed during runtime, making it easy to store and manage elements of variable length.
- Random access: Elements in a vector can be accessed quickly using the array-style indexing syntax, making it possible to retrieve any element in constant time.
- Ease of use: The vector data structure provides a variety of useful functions and operations, such as push_back(), pop_back(), clear(), begin(), end(), etc., that make it easy to manipulate the elements in the vector.
Overall, vector is a versatile and efficient data structure that is widely used in C++, especially when the size of a collection of elements is unknown or changes dynamically.
Code:
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
if (digits.empty()) {
return result;
}
vector<string> mapping = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
result.push_back("");
for (int i = 0; i < digits.size(); i++) {
int num = digits[i] - '0';
while (result.front().size() == i) {
string t = result.front();
result.erase(result.begin());
for (char c : mapping[num]) {
result.push_back(t + c);
}
}
}
return result;
}
};
Explanation:
- First, we create a vector mapping to store the mapping of each digit to its corresponding letters.
- If the input digits is empty, we return an empty result.
- We initialize the result with an empty string and push it into the result vector.
- In the for loop, we iterate through each digit in digits.
- For each digit, we use a while loop to retrieve the first string in the result vector.
- Then, we erase this string from the result vector and add all possible letter combinations of this digit to the result vector.
- Repeat this process until the size of the first string in the result vector is equal to the current index of the for loop.
- Finally, we return the result vector.
Comments
Post a Comment