Problem · String
Telephone Keypad String Encoding
Learn this problemProblem statement
Encode text using a traditional telephone keypad. Letters on keys 2 through 9 require one through four presses according to their position on the key.
- Ignore spaces and treat uppercase and lowercase letters identically.
- If two consecutive encoded letters use the same key, insert
#between their digit sequences.
Return the complete encoded string.
Function
encodeKeypad(text: String) → StringExamples
Example 1
text = "AB"return = "2#22"A and B share key 2, so a separator is inserted.
Example 2
text = "HEY EPIC"return = "4433999337444222"The space is ignored and no adjacent encoded letters share a keypad key.
Constraints
1 <= text.length <= 100000textcontains English letters and spaces.textcontains at least one letter.