Integer to English Words LeetCode

In computer programming and problem-solving, the challenge of converting English words into integers is intriguing. While this may seem straightforward at first glance, the challenge of translating numerical values into their English counterparts quickly becomes apparent, especially considering edge cases.

In this Answer, we’ll construct a Python function that converts a given integer into its word representation. An example of Integer to English Words LeetCode problem is given in the illustration below:

An example of the input and the expected output
An example of the input and the expected output

Problem statement

This problem tasks us with converting a non-negative integer into its English word representation. Let’s look at this problem through an example.

When numberToWords(886752) is called, it should convert the number 886752 into its word representation and print it. In this case, the output should be eight hundred eighty six thousand seven hundred fifty two.

Note: To solve this problem, it may be useful to use one or more lists to store the textual representations of numbers.

Constraints:

  • 00 \leq num 231\leq 2^{31}

Example

canvasAnimation-image
1 of 3

Knowledge test

Attempt this quiz to test your knowledge of this problem:

1

What should be the output of numberToWords(104)?

A)

one hundred four

B)

fourteen

C)

An error

Question 1 of 20 attempted

Algorithm

The algorithm for this problem is as follows:

  • numberToWords is the main function. We’ll handle the base case of 0 and define multiple lists for the textual representations of the numbers.

  • helper is a nested function that handles different ranges of numbers and converts them into words.

  • Check the number’s magnitude and break it into hundreds, thousands, millions, or billions to convert each segment into words.

  • Finally, return the word representation of the number.

886752 is the number we need to convert. As it's less than a million, we need to split it from the thousands. For this reason, we'll perform both division and modulo on this number.
886752 is the number we need to convert. As it's less than a million, we need to split it from the thousands. For this reason, we'll perform both division and modulo on this number.
1 of 4

Educative-99 helps you solve complex coding problems like Integer to English Words by teaching you to recognize patterns and apply the right algorithms.

Coding example

The code for this problem is provided below:

def numberToWords(num: int) -> str:
if num == 0:
return "zero"
onetotwenty = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
ten = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
def helper(num: int) -> str:
if num < 20:
s = onetotwenty[num]
elif num < 100:
s = ten[num // 10] + " " + onetotwenty[num % 10]
elif num < 1000:
s = helper(num // 100) + " hundred " + helper(num % 100)
elif num < 1000000:
s = helper(num // 1000) + " thousand " + helper(num % 1000)
elif num < 1000000000:
s = helper(num // 1000000) + " million " + helper(num % 1000000)
else:
s = helper(num // 1000000000) + " billion " + helper(num % 1000000000)
return s
return helper(num)
result = numberToWords(886752)
print(result)

Code explanation

This code can be explained as follows:

  • Line 1: Define the numberToWords function.

  • Lines 2–3: Handle the base case of 0. Returns "zero".

  • Lines 5–6: Store the textual versions of the numbers between 1 to 20 in a list.

  • Line 7: Store the textual versions of the tens between 10 to 90 in a list.

  • Line 9: Define our helper function to convert integers to English words.

  • Lines 10–11: If num is less than 20, its word representation is retrieved from onetotwenty.

  • Lines 12–13: If num is between 20 and 99, concatenates the word representation of the tens place (ten[num // 10]) with the word representation of the ones place (onetotwenty[num % 10]).

  • Lines 14–15: If num is between 100 and 999, use the word hundred and recursively call itself for the remaining digits.

  • Lines 16–21: This pattern continues for larger numbers, up to billions. In each instance, the number will be split into smaller segments with the appropriate suffixes (thousand, million, billion).

  • Lines 23–25: Return the result.

  • Lines 27–28: Code to test our function.

Note: You may alter the code at the end to test with your own values. 

Time complexity

The code operates with a time complexity of O(n)O(n). This is due to the recursive breakdowns based on digit size, making the verbal equivalent of the number, and handling cases from single digits to billions.

Space complexity

The space complexity for this code is O(n)O(n) as well. It stores word representations of numbers in memory during the recursive calls, scaling proportionately to the number of digits in the input number.




Free Resources

Copyright ©2025 Educative, Inc. All rights reserved