Strings · Easy

238. Convert a Sentence to Its Mobile Numeric Keypad Sequence

Topics: String

Solve it on DeVert Campus - free code runner

Problem

Given a sentence s of uppercase English letters and spaces, convert it to the digit sequence you would type on an old numeric-keypad mobile phone, where each key is pressed a number of times equal to the letter's position within that key's group: 2 -> A(1) B(2) C(3); 3 -> D(1) E(2) F(3); 4 -> G(1) H(2) I(3); 5 -> J(1) K(2) L(3); 6 -> M(1) N(2) O(3); 7 -> P(1) Q(2) R(3) S(4); 8 -> T(1) U(2) V(3); 9 -> W(1) X(2) Y(3) Z(4); 0 -> space (1 press). Print the concatenation of every character's keypress digits, in order, with no separators.

Input and output

Input: The input is a single line containing the sentence s of uppercase English letters and spaces. Output: Print the concatenated keypad digit sequence for s, with no separators.

Example

Input:
SKY
Output:
777755999

Constraints

1 <= |s| <= 1000, s consists of uppercase English letters and spaces

Key observation

Space is explicitly a real, always-present character in this mapping (needing exactly one press) - it should never be silently skipped or dropped when converting a sentence with multiple words.

Intuition

Convert a sentence of capital letters into the sequence of number-key presses you'd need on an old-style mobile phone keypad to type it. Brute force: Compute each letter's key and press-count on the fly with conditional logic (checking letter ranges) every single time it's needed - this works but is more error-prone and slower than building the lookup table just once upfront. Optimised: Building a fixed lookup table ONCE (mapping each of the 26 letters plus space directly to its keypress string) turns every subsequent character conversion into an O(1) table lookup - a single linear pass through the sentence then produces the full answer with no repeated computation.

Brute Force - Linear Search Through Key Groups

Keep the keypad layout as a list of (digit, letters-on-that-key) groups. For every character in the sentence, scan through this list of groups until you find the one containing that character, then scan within that group's letters to find the character's position (number of presses needed), and append that many copies of the digit to the output. This is correct but redoes a linear search through the whole keypad layout for every single character in the sentence. Time: O(n * k) Space: O(1)

Better - Precomputed Hash Map Lookup

Build a hash map from character to its exact digit-press string once, by iterating over the fixed keypad groups a single time up front (e.g. 'A' -> "2", 'B' -> "22", 'C' -> "222", ... '0' for space). Then process the sentence in one pass, looking up each character's precomputed press string in O(1) and appending it to the output. Precomputing the mapping once removes the repeated linear scans over the keypad layout that the brute approach pays for every character. Time: O(n) Space: O(1)

Optimal - Direct Array Indexing with a String Builder

Precompute the same character-to-digits mapping, but store it in a fixed-size array indexed directly by character code (e.g. 'A' - 'A', or ' ' mapped to a reserved slot) instead of a hash map, avoiding any hashing overhead on lookup. Process the sentence in one pass, indexing directly into the array and appending each result into a preallocated string builder/buffer rather than repeated string concatenation. This keeps the same single linear pass as the better approach while removing both hash-computation overhead and the cost of repeated string reallocation. Time: O(n) Space: O(1)

Step by step

1. Build a fixed lookup table mapping every letter (and space) to its exact keypress digit-string, based on the given key groupings. 2. Walk through the sentence character by character. 3. For each character, look up its keypress string and append it to the result.

Complexity

Time: O(|s|) - one lookup and append per character of the sentence, after the one-time fixed table setup. Space: O(1) for the fixed lookup table (always sized to 27 entries: 26 letters + space), plus O(total keypresses) for the output string.

Edge cases

The letters S and Z, which each require 4 key presses (the maximum); spaces, which map to a single '0' and must never be dropped; s up to 1000 characters for performance.

Interview tip

Be precise that 'S' and 'Z' need FOUR presses each (they're the fourth letter of keys 7 and 9 respectively) - a very common detail to get wrong if working from memory rather than the exact given mapping.

All DSA problems