Integer to English Words

Hard

Topics
MathStringRecursion

Convert a non-negative integer n to its English words representation.

Words are separated by single spaces, with no commas and no hyphens: 45 is "Forty Five", not "Forty-Five". Each word is capitalised. 0 is "Zero".

Do not include a scale word for a group that is zero: 1000000 is "One Million", not "One Million Zero Thousand".

Example 1

123
spelled out
English groups digits in threes, so the number splits into the chips above and each is spelled with the same vocabulary plus its scale word. No group here is zero, so every scale word appears.
One chip per group of three digits, most significant first.
Input:  n = 123
Output: "One Hundred Twenty Three"
Explanation: No "and" anywhere, and no hyphen in Twenty Three.

Example 2

12 Thousand345
spelled out
Input:  n = 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Explanation: The thousands group is spelled out, then its scale word.

Example 3

1 Million234 Thousand567
spelled out
Input:  n = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Explanation: Three groups of three digits, each followed by its scale word except the last.

Example 4

1 Million0 Thousand10
spelled outzero group, contributes nothing
Here 1 group is zero and must disappear entirely — scale word included, which is the rule most answers get wrong.
Input:  n = 1000010
Output: "One Million Ten"
Explanation: The thousands group is zero, so no "Thousand" appears at all.

Constraints

  • 0 <= n <= 2^31 - 1
Editorial — intuition, approach, complexity and pitfalls

Intuition

English numbers are grouped in threes, and that is the whole structure of the problem. 1,234,567 is read as "one million", "two hundred thirty four thousand", "five hundred sixty seven" — the same three-digit spelling repeated, with a scale word after each group.

So one function that spells any number from 1 to 999 is enough, and the top level just calls it on each group and tacks on Thousand, Million or Billion. Nothing above 999 needs its own vocabulary.

Inside a group there are three irregularities, and they are the only ones: the teens 10-19 have their own names, the multiples of ten have their own names, and everything else is a tens word plus a units word. A table of 20 small words and a table of 10 tens words covers all of it.

The two rules that catch people out are about absence rather than presence. A zero group contributes nothing, not even its scale word, and zero itself is the only input where the answer is the word "Zero" — everywhere else a zero is silent.

Approach

  1. Return "Zero" immediately for 0, which is the one case the rest of the logic cannot produce.
  2. Write a helper that spells 1-999: if under 20 use the small-words table; if under 100 use the tens word plus, when the remainder is non-zero, the units word; otherwise the hundreds digit plus Hundred plus the helper on the remainder.
  3. At the top level, split the number into groups of three from the least significant end, pairing each with its scale word: nothing, Thousand, Million, Billion.
  4. Spell each non-zero group, append its scale word, and join the pieces from most significant to least with single spaces. Skip zero groups entirely.

Walkthrough

n = 1000010

groups of three, least significant first
   group 0:  10   scale (none)
   group 1:   0   scale Thousand
   group 2:   1   scale Million

spelling each, skipping zeros
   group 2:  "One"  + " Million"   -> "One Million"
   group 1:  zero, contributes nothing
   group 0:  "Ten"                 -> "Ten"

join most significant first
   "One Million Ten"

The skipped group is the point: "One Million Zero Thousand
Ten" would be the result of spelling every group.

Complexity

  • Time — O(1): at most four groups of three digits, each spelled with a bounded number of table lookups. The input is capped at about two billion.
  • Space — O(1): the tables are fixed and the output is bounded.

Common Pitfalls

  • Emitting a scale word for a zero group. 1000010 becomes "One Million Zero Thousand Ten" and every solution hits this on its first test.
  • Forgetting that 0 has to be handled before anything else. The group loop never runs for it, so the answer comes back empty.
  • Adding a trailing or doubled space when a group’s remainder is zero — "Twenty " for 20, or "One Hundred Twenty". Only append the separator when there is something to separate.
  • Treating the teens as "ten one" through "ten nine". They are irregular and need their own table entries.
  • Hyphenating. "Forty-Five" is correct English and the wrong answer here.

Full Solution — all languages

class Solution:
    def numberToWords(self, n):
        SMALL = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
                 "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
                 "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
        TENS = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
                "Eighty", "Ninety"]
        SCALES = ["", "Thousand", "Million", "Billion"]

        if n == 0:
            return "Zero"

        def under_thousand(x):
            if x < 20:
                return SMALL[x]
            if x < 100:
                rest = SMALL[x % 10]
                return TENS[x // 10] + ((" " + rest) if rest else "")
            rest = under_thousand(x % 100)
            return SMALL[x // 100] + " Hundred" + ((" " + rest) if rest else "")

        pieces = []
        scale = 0
        while n > 0:
            group = n % 1000
            if group:
                words = under_thousand(group)
                if SCALES[scale]:
                    words += " " + SCALES[scale]
                pieces.append(words)
            n //= 1000
            scale += 1

        return " ".join(reversed(pieces))
SensAI
Beta
Tap the mic to stop voice mode