← DSA Atlas
Dedicated problem page · #273

Integer to English Words

HardRandomization, Math and Miscellaneous (FAANG add-on)Chunk-by-thousands decompositionRecursive grouping with word lookup tables
Solve on LeetCode ↗
273
HardRandomization, Math and Miscellaneous (FAANG add-on)Recursive grouping with word lookup tablesChunk-by-thousands decomposition

Integer to English Words

Convert a non-negative integer into its English words representation. Break the number into groups of three digits and append the scale word (Thousand, Million, Billion) for each group.

Open official problem prompt ↗
In plain English

Turn a bounded non-negative integer into its exact English-words spelling with correct scale words.

Picture it like this

Reading a check aloud: you speak each group of three digits and tack on 'thousand', 'million', and so on.

Example
Input
num = 1234567
Output
"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Why
1234567 splits into 1 (Million), 234 (Thousand), 567, each spelled out and joined with its scale word.
Constraints
0 <= num <= 2^31 - 1
Pattern lesson

See the pattern, then code

Chunk-by-thousands decomposition
Recognition clue

English number names naturally decompose into three-digit groups with scale suffixes, signaling a chunk-by-1000 conversion using fixed lookup tables.

Randomization, Math and Miscellaneous (FAANG add-on)

Uniform random selection, sampling streams, number theory, or precision-heavy parsing.. Any three-digit chunk is spelled by combining hundreds, tens (with irregular teens), and units; process chunks from least significant, prepend each chunk's words plus the matching scale word.

New words, made simpleKnow these before the algorithm
Chunk
A group of three digits (0-999) processed as a unit.
Scale word
Thousand, Million, or Billion appended per chunk based on its position.
Teens irregularity
Numbers 10-19 have unique names, requiring a dedicated lookup range.
Approaches from first idea to best ideaCompare the trade-offs
ApproachHow it thinksCost
Digit-by-digit ad hoc

Error-prone for teens, hundreds, and scale placement.

Handle each digit with sprawling conditionals.

Time O(1)Space O(1)
The rule we keep true

Invariant

After processing chunk index i, res holds the fully spelled words for all digits from position 3*i onward, with correct scale words attached.

Why this is correct

Reasoning

English grouping is periodic every three digits with a fixed scale suffix, so spelling each sub-1000 chunk independently and concatenating with the position's scale word reconstructs the full name; skipping all-zero chunks avoids stray scale words.

The algorithm in three movesSay these aloud before coding
1Return 'Zero' when num is 0

chunk 567 -> Five Hundred Sixty Seven

2Define lookup tables for values below twenty, tens, and thousand-scale words

chunk 234 + Thousand

3Write a helper that spells any value under 1000

chunk 1 + Million

4Loop over 1000-sized chunks, prepending helper output and the scale word, then strip trailing spaces

Visual trace

Follow the data, one decision at a time

Input snapshotHighlighted cells are involved in this example
10
2341
5672
1 · Read1234567 % 1000 = 567
2 · AskSpell it?
3 · Update statehelper -> "Five Hundred Sixty Seven "
4 · Resultres = "Five Hundred Sixty Seven ", num=1234.
Key takeaway

1234567 split into three-digit chunks with scale words Million and Thousand.

Code walkthrough

Read the solution in small chunks

Python 3

Do not memorize the whole program. Connect each group of lines to one job in the algorithm.

  1. 1
    Lines 3-4Zero case

    Zero is the one value with no chunk output, spelled directly.

  2. 2
    Lines 5-11Lookup tables

    Cover units/teens, tens, and scale words.

  3. 3
    Lines 13-21Sub-1000 helper

    Recursively spell hundreds, then tens, then units.

  4. 4
    Lines 23-31Chunk loop

    Prepend each nonzero chunk's words plus scale word, then strip surrounding whitespace.

Make it stick

Edge cases, mistakes, and one self-check

E

Edge cases to test

  • num = 0 -> "Zero"
  • Round thousands like 1000000 -> "One Million" with no stray words for zero chunks
  • Numbers with internal zero chunks such as 1000000 (skip the empty middle chunk)
  • Maximum 2147483647 -> up to Billion scale
!

Common beginner mistakes

  • Emitting a scale word for an all-zero chunk
  • Mishandling the teens (10-19) with the tens table
  • Leaving double or trailing spaces without a final strip
  • Forgetting the standalone Zero case
Check your understanding

Why is the chunk skipped when num % 1000 == 0?