Convert a non-negative integer num to its English words representation.
Example 1:
Input: num = 123 Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Constraints:
0 <= num <= 231 - 1The key idea in #273 Integer to English Words is to convert a number into its English representation by processing it in groups of three digits (hundreds). Numbers in English naturally follow a hierarchical structure such as Billion → Million → Thousand → Hundred. By breaking the integer into these segments, you can convert each chunk separately and append the appropriate scale word.
A common approach is to use recursion or helper functions that translate numbers less than 1000 into words. Predefined mappings for numbers below 20 and multiples of ten (20, 30, 40, etc.) simplify the conversion. For each segment, construct the phrase and combine it with the corresponding unit like "Thousand" or "Million".
This method keeps the logic modular and readable while handling edge cases like zero or trailing spaces. Because the number of digit groups is limited, the algorithm runs in O(log n) time relative to the number of digits, with small auxiliary space used for recursion and string construction.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Recursive chunk processing (groups of three digits) | O(log n) | O(log n) |
| Iterative grouping with helper mappings | O(log n) | O(1) auxiliary (excluding output string) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
This approach utilizes recursion to convert the integer into segments of hundreds, thousands, millions, and billions, and then converts each of these segments into words using a predefined dictionary. This method divides and conquers the problem by handling one segment at a time.
Time Complexity: O(1), since the number of segments is constant. Space Complexity: O(1), due to constant space usage for building the output string.
1def numberToWords(num):
2 if num == 0:
3 return 'Zero'
4
5 below_20 = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
6 tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
7 thousands = ['', 'Thousand', 'Million', 'Billion']
8
9 def helper(n):
10 if n == 0:
11 return []
12 elif n < 20:
13 return [below_20[n]]
14 elif n < 100:
15 return [tens[n // 10]] + helper(n % 10)
16 else:
17 return [below_20[n // 100]] + ['Hundred'] + helper(n % 100)
18
19 res = []
20 for i, thousand in enumerate(thousands):
21 if num % 1000 != 0:
22 res = helper(num % 1000) + [thousand] + res
23 num //= 1000
24
25 return ' '.join([word for word in res if word])This Python solution decomposes the number recursively into segments of 1000 using integer division and modulus operations. The function helper converts numbers below 1000 to words. The main function iterates over increasing powers of thousand, converts each segment, and concatenates them with appropriate thousand scales.
An iterative approach using a stack can sequentially build the English words representation by handling one segment of the number at a time. This non-recursive method is useful in avoiding too deep recursion especially with large numbers.
Time Complexity: O(1), as the number of operations is fixed for a constant set of segments. Space Complexity: O(1), using constant space for string manipulation.
1var numberToWords = function(num) {
2 if (num ===
This approach breaks down the number into smaller chunks of thousands and recursively converts each chunk into words. By handling different magnitudes separately, such as thousands, millions, etc., the problem becomes more manageable.
Time Complexity: O(log(num)) - Because the number is processed in chunks of thousands.
Space Complexity: O(log(num)) - Due to the recursion stack when handling each chunk.
1class Solution:
2 def numberToWords(self, num: int) ->
This approach uses an iterative method to convert the integer into words by dividing the number into parts less than 1000 and mapping each part to its English equivalent. The algorithm keeps updating the resultant string by concatenating appropriate word segments.
Time Complexity: O(log(num)) - Efficient handling by breaking number into segments.
Space Complexity: O(1) - Iterative approach minimizes auxiliary space usage.
1public class Solution {
2 private final String[] below20 =
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, this problem is considered a classic string and number manipulation challenge and has appeared in interviews at major tech companies. It tests recursion, edge-case handling, and the ability to design clean helper functions.
Hash maps or arrays are typically used to store mappings for numbers below 20 and multiples of ten such as Twenty, Thirty, and Forty. These lookups make it easy to construct the English phrase while recursively breaking down the number.
The optimal approach splits the number into groups of three digits and converts each segment into English words using helper mappings. Recursion or a helper function is commonly used to process numbers below 1000 and append scale words like Thousand, Million, and Billion.
English number naming naturally groups digits in sets of three such as Thousand, Million, and Billion. Processing numbers in these chunks simplifies the logic and allows each segment to be converted independently before combining them.
This JavaScript solution uses iteration and a helper function to convert each segment of the number into English words. Similar to the recursive solution, it iteratively processes segments in the order of ones, thousands, millions, and so on. This approach uses string concatenation and avoids recursion depth issues.
This solution uses recursion to handle chunks of up to three digits: ones, tens, and hundreds, separately. It utilizes lists to map integer values to their corresponding English word equivalents for numbers up to nineteen, tens, and big chunks (thousands, millions, billions). When a chunk is processed, it concatenates the word value, and processes recursively for larger chunks.
The solution involves converting each part of the number separated by thousands into words iteratively. It uses arrays to map numbers to words, efficiently processing up to two-digit numbers, and recursively handling hundreds, enhancing both clarity and performance.