# Phone Number Mnemonics ⇒ can use word and index to create each case without concatenation

![](/files/NfmHTPIDfZBpXy4q0IXU)

* first try
* time: 4^n x n
  * n: length of phonenumber
  * each number ⇒ has max 4 different cases ⇒ each case run
    * n- 1, n - 2, …. ⇒ n
    * each time 4^n happens
* space: 4^n cases, each case has length of n

concat part increase time

```jsx
const keypad = {
  0: '0',
  1: '1',
  2: 'abc',
  3: 'def',
  4: 'ghi',
  5: 'jkl',
  6: 'mno',
  7: 'pqrs',
  8: 'tuv',
  9: 'wxyz',
};

let mnemonics;
function phoneNumberMnemonics(phoneNumber) {
  mnemonics = [];
  buildMnemonic(phoneNumber);
  return mnemonics;
}

function buildMnemonic(phoneNumber, idx = 0, curr = []) {
  if (phoneNumber.length === curr.length) {
    mnemonics.push(curr.join(''));
    return;
  }

  const number = Number(phoneNumber[idx]);
  const chars = keypad[number];

  for (let i = 0; i < chars.length; i++) {
    buildMnemonic(phoneNumber, idx + 1, curr.concat(chars[i]));
  }

  return;
}

// Do not edit the line below.
exports.phoneNumberMnemonics = phoneNumberMnemonics;
```

* second try
* removed concat

using word and its index to store each letter,

* 4^n x n, 4^n x n

```jsx
const keypad = {
  0: '0',
  1: '1',
  2: 'abc',
  3: 'def',
  4: 'ghi',
  5: 'jkl',
  6: 'mno',
  7: 'pqrs',
  8: 'tuv',
  9: 'wxyz',
};

let mnemonics;
function phoneNumberMnemonics(phoneNumber) {
  const word = new Array(phoneNumber.length).fill('0');
  mnemonics = [];
  buildMnemonic(phoneNumber, 0, word);
  return mnemonics;
}

function buildMnemonic(phoneNumber, idx = 0, word = []) {
  if (phoneNumber.length === idx) {
    mnemonics.push(word.join(''));
    return;
  }

  const number = Number(phoneNumber[idx]);
  const chars = keypad[number];

  for (let i = 0; i < chars.length; i++) {
    word[idx] = chars[i];
    buildMnemonic(phoneNumber, idx + 1, word);
  }

  return;
}

// Do not edit the line below.
exports.phoneNumberMnemonics = phoneNumberMnemonics;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/phone-number-mnemonics-can-use-word-and-index-to-create-each-case-without-concatenation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
