Integer to English Words
Hard
Topics
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
Input: n = 123 Output: "One Hundred Twenty Three" Explanation: No "and" anywhere, and no hyphen in Twenty Three.
Example 2
Input: n = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Explanation: The thousands group is spelled out, then its scale word.
Example 3
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
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
- Return
"Zero"immediately for 0, which is the one case the rest of the logic cannot produce. - 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
Hundredplus the helper on the remainder. - 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. - 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.
1000010becomes "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))import java.util.*;
class Solution {
public String numberToWords(int n) {
if (n == 0) return "Zero";
List<String> pieces = new ArrayList<>();
int scale = 0;
long v = n;
while (v > 0) {
int group = (int) (v % 1000);
if (group != 0) {
String words = underThousand(group);
if (!SCALES[scale].isEmpty()) words += " " + SCALES[scale];
pieces.add(words);
}
v /= 1000;
scale++;
}
Collections.reverse(pieces);
return String.join(" ", pieces);
}
private static final String[] SMALL = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private static final String[] TENS = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private static final String[] SCALES = {"", "Thousand", "Million", "Billion"};
private String underThousand(int x) {
if (x < 20) return SMALL[x];
if (x < 100) {
String rest = SMALL[x % 10];
return TENS[x / 10] + (rest.isEmpty() ? "" : " " + rest);
}
String rest = underThousand(x % 100);
return SMALL[x / 100] + " Hundred" + (rest.isEmpty() ? "" : " " + rest);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: string numberToWords(int n) {
vector<string> SMALL = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
vector<string> TENS = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
vector<string> SCALES = {"", "Thousand", "Million", "Billion"};
if (n == 0) return "Zero";
function<string(int)> underThousand = [&](int x) -> string {
if (x < 20) return SMALL[x];
if (x < 100) {
string rest = SMALL[x % 10];
return TENS[x / 10] + (rest.empty() ? "" : " " + rest);
}
string rest = underThousand(x % 100);
return SMALL[x / 100] + " Hundred" + (rest.empty() ? "" : " " + rest);
};
vector<string> pieces;
int scale = 0;
long long v = n;
while (v > 0) {
int group = (int)(v % 1000);
if (group) {
string words = underThousand(group);
if (!SCALES[scale].empty()) words += " " + SCALES[scale];
pieces.push_back(words);
}
v /= 1000;
scale++;
}
string out;
for (int i = (int)pieces.size() - 1; i >= 0; i--) {
if (!out.empty()) out += " ";
out += pieces[i];
}
return out;
}
};/**
* @param {number} n
* @return {string}
*/ var numberToWords = function(n) {
const SMALL = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
const TENS = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
const SCALES = ["", "Thousand", "Million", "Billion"];
if (n === 0) return "Zero";
const underThousand = (x) => {
if (x < 20) return SMALL[x];
if (x < 100) {
const rest = SMALL[x % 10];
return TENS[Math.floor(x / 10)] + (rest ? " " + rest : "");
}
const rest = underThousand(x % 100);
return SMALL[Math.floor(x / 100)] + " Hundred" + (rest ? " " + rest : "");
};
const pieces = [];
let scale = 0;
while (n > 0) {
const group = n % 1000;
if (group) {
let words = underThousand(group);
if (SCALES[scale]) words += " " + SCALES[scale];
pieces.push(words);
}
n = Math.floor(n / 1000);
scale++;
}
return pieces.reverse().join(" ");
};