Ctrl K

Reassigning the Phone Keypad

Medium
On an old push-button phone, letters are typed with keys 2 through 9. Each key holds its own group of letters: one press gives the first letter of the group, two presses give the second, and so on. You are free to redistribute the letters among the keys: every letter must land on exactly one key at some position in its group, and a key may hold any number of letters (or none). Keys 0, 1, * and # carry no letters. Given a string text of lowercase English letters, choose a layout that makes typing text take as few presses as possible, and return that number.
Example 1
text="banana" → 6

Each of the three letters goes first on its own key: 3 + 2 + 1 = 6.

Example 2
text="abcdefghij" → 12

Eight letters go first on the eight keys, the other two go second: 8·1 + 2·2 = 12.

Example 3
text="qqqq" → 4
Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNo Twin Letters Side by Side MediumNine-Button Layout MediumPalindromes From Shared Letters MediumFill the Blanks With the Fewest Repeats MediumEven Out Letter Counts Medium
loading Python…
Input
text="banana"
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 50 tests on the server, hidden ones included.