cswork / libraryOpen practice workspace

Coding questions · Epic

Encode Letters with a Telephone Keypad

Encode letters as telephone keypad presses, ignoring case and spaces and inserting a separator whenever consecutive encoded letters share the same key.

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.

KeyLetter encodings
2A: 2, B: 22, C: 222
3D: 3, E: 33, F: 333
4G: 4, H: 44, I: 444
5J: 5, K: 55, L: 555
6M: 6, N: 66, O: 666
7P: 7, Q: 77, R: 777, S: 7777
8T: 8, U: 88, V: 888
9W: 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 = "AB"
Output: "2#22"

A and B share key 2, so their encodings need a separator.

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.

Encode Letters with a Telephone Keypad

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.

Hints

  1. An ignored space does not break adjacency between letters.
  2. The keys for P through S and W through Z each have four letters.

Approach

Store the 26 encodings from the supplied keypad table. Scan the input from left to right, skipping spaces and converting each remaining letter to uppercase. Look up that letter's encoding and compare its first digit with the previous encoded letter's key. Append one # exactly when the two keys match, then append the encoding and remember its key. Skipping a space leaves the remembered key unchanged, so the stated adjacency rule is preserved. Before each non-space letter, the accumulated output is exactly the encoding of the letters already processed, with precisely the required separators; the next step preserves this invariant. The initial state has no previous key, preventing a leading separator, and separators are added only immediately before an encoding, preventing a trailing separator. Empty and all-space inputs leave the output empty. Each input character causes constant work and at most five output characters; collecting pieces or using a growing string builder gives O(n) time and O(n) output storage.

  • Time complexity: O(n)
  • Space complexity: O(n), including output and the piece buffer where used.