# My iterative sollution, very simple under 15 lines

• This is my solution, FYI

``````vector<string> letterCombinations(string digits) {
vector<string> res;
string charmap[10] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
res.push_back("");
for (int i = 0; i < digits.size(); i++)
{
vector<string> tempres;
string chars = charmap[digits[i] - '0'];
for (int c = 0; c < chars.size();c++)
for (int j = 0; j < res.size();j++)
tempres.push_back(res[j]+chars[c]);
res = tempres;
}
return res;
}``````

• nice way to convert digits to string

• Concise code!

• very simple!

• I think the time complexity is too high compared to the backtracking method because for each digit you must traverse the vector res, which adds additional complexity.

• This post is deleted!

• Very clear code!but I found a small problem, if the input digits is an empty string, this code returns [""] while the answer should be []

• Just add "if(digits.size() == 0) return res;" in the back of "vector<string> res;" is OK.

• if the input is "1",what is expected output? "1" or NULL.
my method output NULL,this one output "1".
but both are passed the oj

• you can add if ((res.size()==1)&&(res[0] == "")) res.clear();

• very elegant bfs solution!

• nice plan, can you give the time complexity of this method

• brilliant solution! thx bro!

• @voidhust

I agree, otherwise it cannot handle the case of empty digits.

In fact, `if (res[0] == "") res.clear();` would be sufficient, since `""` has been pushed into res in the first place.

• Include the corner case: if(digits.size()==0) return res;

• you must traverse the vector res, which adds additional complexity.

can you explain it detailedly

• This is the same idea as BFS. Use a queue instead could avoid copying the whole vector<string> in each depth.

• why do you need `res.push_back("");`?

• Can I know the time consumed? I have a iterative DFS version which costs 0 ms, but very complex.

• Had the same idea. But it turns out this solution is 2X slower than simple DFS.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.