Skip to the code
cf.
+25 XP

#655 Rebase a Number String

medium

A developer tool converts IDs between bases. digits is a non-negative number written in base fromBase, using 0-9 then a-z (upper case letters mean the same as lower case). It may have leading zeros and may be far larger than 2^53.

Write rebase(digits, fromBase, toBase) that returns the same number written in base toBase, using lowercase letters and no leading zeros (zero is "0").

Example 1

Input: digits = "ff", fromBase = 16, toBase = 2
Output: "11111111"

ff is 255, which is eight 1s in binary.

Example 2

Input: digits = "101", fromBase = 2, toBase = 10
Output: "5"

1 × 4 + 0 × 2 + 1 = 5.

Constraints

  • 2 <= fromBase, toBase <= 36
  • 1 <= digits.length <= 100
  • Every character is a valid digit in fromBase.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

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