All problems
0243HardMathStringRecursion

Spell the Voucher Amount

Tracked in this browser only
Write code

Trains the technique from

LeetCode 273Integer to English Words

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A payments desk prints vouchers, and each voucher carries its amount spelled out in words beside the figures. Given a non-negative integer amount, return the words the printer should put on the voucher.

The printer's vocabulary is fixed. Single figures and teens: 1 One, 2 Two, 3 Three, 4 Four, 5 Five, 6 Six, 7 Seven, 8 Eight, 9 Nine, 10 Ten, 11 Eleven, 12 Twelve, 13 Thirteen, 14 Fourteen, 15 Fifteen, 16 Sixteen, 17 Seventeen, 18 Eighteen, 19 Nineteen. Whole tens: 20 Twenty, 30 Thirty, 40 Forty, 50 Fifty, 60 Sixty, 70 Seventy, 80 Eighty, 90 Ninety. It also holds the words Hundred, Thousand, Million, Billion and Zero.

The printer works to these rules.

  1. An amount of 0 prints as Zero. For every other amount the word Zero is never printed.
  2. Cut the figures into groups of three digits, counting from the right, so the leftmost group may be short. Read from the left, the groups are tagged Billion, Million, Thousand, and the rightmost group carries no tag.
  3. A group whose digits are all 0 prints nothing whatsoever, tag included.
  4. Any other group prints, in this order: the word for its hundreds digit followed by Hundred when that digit is not 0; then the number formed by its last two digits, as one word when that number is 1 through 19, as the whole-tens word plus the units word when the units digit is not 0, as the whole-tens word by itself when the units digit is 0, and as nothing at all when both digits are 0. The group's tag comes last.
  5. Exactly one space separates neighbouring words. No hyphens, no commas, no and, and no space at either end of the answer.

Examples

Example 1

Input
amount = 907
Output
"Nine Hundred Seven"

There is one group, 907. Its hundreds digit is 9, which prints Nine Hundred, and its last two digits form 7, which prints Seven.

Example 2

Input
amount = 6042519
Output
"Six Million Forty Two Thousand Five Hundred Nineteen"

The groups are 6, 042 and 519, tagged Million, Thousand and nothing. They print Six Million, then Forty Two Thousand, then Five Hundred Nineteen.

Example 3

Input
amount = 5000018
Output
"Five Million Eighteen"

The groups are 5, 000 and 018. The middle group is all zeros, so it prints nothing and its Thousand tag goes with it, leaving Five Million Eighteen.

Constraints

  • 0 <= amount <= 2^31 - 1

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def spell_amount(amount: int) -> str:
Java
public String spellAmount(int amount)
September 7
Apply