242/670

242. Integer to English Words

Hard

You are given a non-negative integer num. Spell it out in English and return the resulting string.

Use the standard short-scale convention: split the digits into groups of three and attach Thousand, Million, or Billion to each non-zero group. Every word starts with a capital letter, words are separated by exactly one space, and the answer never contains hyphens, commas, or the word "and" — so 45 becomes Forty Five and 123 becomes One Hundred Twenty Three. If num is 0, return Zero.

Example 1:

Input: num = 123

Output: "One Hundred Twenty Three"

Explanation: One group of three digits: a hundreds digit (One Hundred), then the tens word (Twenty), then the units word (Three).

Example 2:

Input: num = 1234567

Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Explanation: The digit groups are 1 | 234 | 567, spelled with their scale words Million and Thousand; the last group carries no scale word.

Constraints:

  • 0 ≤ num ≤ 2³¹ - 1

Hints:

Numbers split naturally into groups of three digits. If you can spell any value from 1 to 999, you can spell everything by appending Thousand, Million, or Billion to each group.

Inside a group there are three regimes: below 20 the names are irregular (Eleven, Twelve, ...), 20–99 is a tens word plus a recursive remainder, and 100–999 is a digit word, Hundred, then the remainder.

Never emit empty pieces: skip any group whose three digits are all zero, and build a list of words that you join with single spaces at the end instead of gluing strings together.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: num = 123

Expected output: "One Hundred Twenty Three"