Skip to the code
+25 XP

#563 Numbers From a Limited Keypad

medium

A kiosk's on-screen keypad only shows some digit keys, given in keys as strings in increasing order (never "0"). A visitor can type any positive number using only those keys, pressing a key as often as they like.

Write countTypable(keys, n) that returns how many positive integers from 1 to n can be typed.

Example 1

Input: keys = ["2","5"], n = 30
Output: 4

2, 5, 22, and 25.

Example 2

Input: keys = ["7"], n = 6
Output: 0

The smallest typable number is 7.

Constraints

  • 1 <= keys.length <= 9
  • keys holds digits from "1" to "9", in increasing order, no repeats
  • 1 <= n <= 10^12
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).