Problem · String

Telephone Keypad String Encoding

Learn this problem
EasyEpic logoEpicFULLTIMEOA

Problem 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) → String

Examples

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 <= 100000
  • text contains English letters and spaces.
  • text contains at least one letter.

More Epic problems

drafts saved locally
public String encodeKeypad(String text) {
    // Write your code here.
}
text"AB"
expected"2#22"
checking account