Loading...
You are given a string s of English letters (either case) and spaces, containing at least one letter.
On a classic phone keypad, each digit from 2 to 9 carries a group of letters, and a letter is typed by pressing its digit repeatedly: once for the first letter of the group, twice for the second, and so on:
| Key | Letters |
|---|---|
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 |
Return the encoding of s as a sequence of key presses, applying these rules:
s are ignored.# between their encodings. Letters on different keys are concatenated directly.The # separator makes the encoding unambiguous: without it, the encoding of "AB" would read 222, which is the encoding of "C".
Input: s = "AB"
Output: "2#22"
Explanation: A and B share key 2, so their encodings 2 and 22 are separated by #.
Input: s = "AD"
Output: "23"
Explanation: A (key 2) and D (key 3) use different keys, so no separator is needed.
Input: s = "HEY EPIC"
Output: "4433999337444222"
Explanation: H → 44, E → 33, Y → 999, E → 33, P → 7, I → 444, C → 222. The space is ignored, and no two consecutive letters share a key, so the encodings are concatenated directly.
s.length ≤104s consists of English letters (uppercase or lowercase) and spacess contains at least one letterClick "Run" to test with sample cases or "Submit" to run all tests.