Encode Letters with a Telephone Keypad
Epic SystemsSoftware Engineer · Online AssessmentLast updated
Tests string manipulation, hash-table lookup, and state tracking by converting an alphanumeric string into multi-tap keypad strokes with pause delimiters.
Encode Letters with a Telephone Keypad
On a standard 12-key telephone keypad, letters are mapped to numbers from 2 to 9 as follows:
2:A, B, C3:D, E, F4:G, H, I5:J, K, L6:M, N, O7:P, Q, R, S8:T, U, V9:W, X, Y, Z
To type a letter in "multi-tap" mode, a user presses a key repeatedly. For example, pressing 2 once produces 'A', twice produces 'B', and three times produces 'C'.
Write a function that encodes a given string into its sequence of keypad button presses according to these rules:
- Case-Insensitive: Uppercase and lowercase letters are encoded identically (e.g.,
'a'and'A'both map to"2"). - Ignore Spaces: Spaces in the input string should be completely ignored.
- Separator between Consecutive Same-Key Letters: If two consecutive encoded letters originate from the same number key, you must insert a space (
' ') between their digit representations to distinguish between multiple presses of the same button versus separate letters. If consecutive letters use different keys, no separator is needed.
Function Signature
def encode_keypad(text: str) -> str:
pass
Input
text: A string consisting of English letters and spaces.
Output
- Returns a string representing the encoded button presses.
Constraints
0 <= len(text) <= 10^5textcontains only ASCII English letters ('a'-'z','A'-'Z') and space characters (' ').
Examples
Example 1:
Input: text = "hello"
Output: "4433555 555666"
Explanation:
- 'h' -> "44" (key 4)
- 'e' -> "33" (key 3)
- 'l' -> "555" (key 5)
- 'l' -> "555" (key 5, same key as previous letter 'l', so insert a space)
- 'o' -> "666" (key 6)
Result: "4433555 555666"
Example 2:
Input: text = "A B"
Output: "2 22"
Explanation:
- Spaces in input are ignored.
- 'A' -> "2" (key 2)
- 'B' -> "22" (key 2, same key as 'A', so insert a space)
Result: "2 22"
Example 3:
Input: text = ""
Output: ""
Hints
Hint 1
Precompute a lookup table mapping each lowercase letter to a pair: the number key it belongs to and the sequence of digits needed to produce it.
Hint 2
Filter out spaces and convert all letters to lowercase before processing, or handle them on the fly.
Hint 3
Track the key of the immediately preceding valid letter; if the current letter's key matches the previous one, append a space delimiter before appending the current letter's digit sequence.
Follow-up questions
- How would you implement the reverse function, `decode_keypad(encoded: str) -> str`, handling single spaces as pauses between same-key characters?
- If the input was a continuous stream of characters and you needed to produce encoded output with O(1) auxiliary memory, how would you structure the solution?
- How would you extend this logic to support full T9 predictive text rather than multi-tap encoding?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.