Encode Letters with a Telephone Keypad
Company: Epic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Encode a string using the traditional telephone keypad mapping below. Each letter becomes the digit for its key repeated according to the letter's position on that key.
### Function
Implement `encodeKeypad(text) -> string`.
The input contains English letters `A` through `Z`, their lowercase forms, and spaces. Ignore letter case and spaces. Encode the remaining sequence of letters from left to right.
| Key | Letter encodings |
| --- | --- |
| 2 | `A: 2`, `B: 22`, `C: 222` |
| 3 | `D: 3`, `E: 33`, `F: 333` |
| 4 | `G: 4`, `H: 44`, `I: 444` |
| 5 | `J: 5`, `K: 55`, `L: 555` |
| 6 | `M: 6`, `N: 66`, `O: 666` |
| 7 | `P: 7`, `Q: 77`, `R: 777`, `S: 7777` |
| 8 | `T: 8`, `U: 88`, `V: 888` |
| 9 | `W: 9`, `X: 99`, `Y: 999`, `Z: 9999` |
### Separator rule
If consecutive letters in the sequence being encoded use the same key, insert exactly one `#` between their encodings. Otherwise, concatenate their encodings directly. An ignored space contributes no output and does not separate the remaining letters. Do not add a leading or trailing `#`.
Return the complete encoded string. If there are no letters to encode, return the empty string.
### Examples
```text
text = "AB"
Output: "2#22"
```
`A` and `B` share key 2, so their encodings need a separator.
```text
text = "AD"
Output: "23"
```
`A` and `D` use different keys, so no separator is needed.
The case and space rules mean that `"a b"` encodes to `"2#22"` as well.
Overview: Encode letters as telephone keypad presses, ignoring case and spaces and inserting a separator whenever consecutive encoded letters share the same key.
Read the full Epic Software Engineer interview experience this question came from
Encode a string using the traditional telephone keypad mapping below. Each letter becomes the digit for its key repeated according to the letter's position on that key.
### Function
Implement `encodeKeypad(text) -> string`.
The input contains English letters `A` through `Z`, their lowercase forms, and spaces. Ignore letter case and spaces. Encode the remaining sequence of letters from left to right.
| Key | Letter encodings |
| --- | --- |
| 2 | `A: 2`, `B: 22`, `C: 222` |
| 3 | `D: 3`, `E: 33`, `F: 333` |
| 4 | `G: 4`, `H: 44`, `I: 444` |
| 5 | `J: 5`, `K: 55`, `L: 555` |
| 6 | `M: 6`, `N: 66`, `O: 666` |
| 7 | `P: 7`, `Q: 77`, `R: 777`, `S: 7777` |
| 8 | `T: 8`, `U: 88`, `V: 888` |
| 9 | `W: 9`, `X: 99`, `Y: 999`, `Z: 9999` |
### Separator rule
If consecutive letters in the sequence being encoded use the same key, insert exactly one `#` between their encodings. Otherwise, concatenate their encodings directly. An ignored space contributes no output and does not separate the remaining letters. Do not add a leading or trailing `#`.
Return the complete encoded string. If there are no letters to encode, return the empty string.
### Examples
```text
text = "AB"
Output: "2#22"
```
`A` and `B` share key 2, so their encodings need a separator.
```text
text = "AD"
Output: "23"
```
`A` and `D` use different keys, so no separator is needed.
The case and space rules mean that `"a b"` encodes to `"2#22"` as well.
Constraints
- text is a finite string containing only English uppercase letters, English lowercase letters and ordinary spaces.
- Ignore letter case and all spaces. No source-specified numeric length cap is added.
- Insert exactly one # between consecutive remaining letters on the same key, even when spaces occurred between them.
- Do not add a leading or trailing #; empty and all-space inputs return the empty string.
Examples
Input: ("AB",)
Expected Output: '2#22'
Explanation: The first source example inserts one separator for the shared key 2.
Input: ("AD",)
Expected Output: '23'
Explanation: The second source example uses different keys and no separator.
Hints
- An ignored space does not break adjacency between letters.
- The keys for P through S and W through Z each have four letters.