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:
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:
num
Example
Knowledge test
Attempt this quiz to test your knowledge of this problem:
What should be the output of numberToWords(104)?
one hundred four
fourteen
An error
Algorithm
The algorithm for this problem is as follows:
numberToWordsis the main function. We’ll handle the base case of0and define multiple lists for the textual representations of the numbers.helperis 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.
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 sreturn helper(num)result = numberToWords(886752)print(result)
Code explanation
This code can be explained as follows:
Line 1: Define the
numberToWordsfunction.Lines 2–3: Handle the base case of
0. Returns"zero".Lines 5–6: Store the textual versions of the numbers between
1to20in a list.Line 7: Store the textual versions of the tens between
10to90in a list.Line 9: Define our
helperfunction to convert integers to English words.Lines 10–11: If
numis less than20, its word representation is retrieved fromonetotwenty.Lines 12–13: If
numis between20and99, 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
numis between100and999, use the wordhundredand 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
Space complexity
The space complexity for this code is
Free Resources