codekofi
← All problems

Problem 18

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1void build(const string& digits, int i, string& current,
2 vector<string>& out) {
3
4 string keys[10] = {"", "", "abc", "def", "ghi",
5 "jkl", "mno", "pqrs", "tuv", "wxyz"};
6
7 int n = digits.size();
8
9 if (i == n) {
10 out.push_back(current);
11 return;
12 }
13
14 for (char ch : keys[digits[i] - '0']) {
15 current.push_back(ch);
16 build(digits, i + 1, current, out);
17 current.pop_back();
18 }
19}
20
21vector<string> solution(const string& digits) {
22
23 vector<string> ans;
24
25 if (digits.empty()) {
26 return ans;
27 }
28
29 string current;
30 build(digits, 0, current, ans);
31
32 return ans;
33}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.