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 - 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for converting a number to English words involves checking every possible combination of number groupings. We look at each digit place and decide what English word or phrase represents it. By checking all possibilities, we piece together the full English phrase.
Here's how the algorithm would work step-by-step:
def integer_to_english(number):
if number == 0:
return 'Zero'
less_than_20 = ['','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']
thousands = ['','Thousand','Million','Billion']
def helper(number):
if number < 20:
return less_than_20[number]
elif number < 100:
return tens[number // 10] + (' ' + less_than_20[number % 10])
else:
return less_than_20[number // 100] + ' Hundred ' + helper(number % 100)
result = []
# Iterate through the place values
for i in range(4):
number_to_process = number % 1000
if number_to_process != 0:
# Translate the group into english words
result.append(helper(number_to_process) + ' ' + thousands[i])
number //= 1000
# Reverse the order and join the words
return ' '.join(result[::-1]).strip()
To convert a number to English words, we break it down into chunks based on powers of 1000 (billions, millions, thousands, ones). We then convert each chunk into words and combine them with the appropriate magnitude word.
Here's how the algorithm would work step-by-step:
def integer_to_english(number):
if number == 0:
return "Zero"
ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
thousands = ["", "Thousand", "Million", "Billion"]
def convert_group_to_words(num):
if num == 0:
return ""
hundreds_place = num // 100
remainder = num % 100
words = ""
if hundreds_place:
words += ones[hundreds_place] + " Hundred "
if remainder < 10:
words += ones[remainder]
elif remainder < 20:
words += teens[remainder - 10]
else:
words += tens[remainder // 10] + " " + ones[remainder % 10]
return words.strip()
result = ""
group_index = 0
# Process groups of three digits
while number > 0:
current_number_group = number % 1000
number //= 1000
# Convert the group to English words
group_words = convert_group_to_words(current_number_group)
# Add the magnitude word if the group is not zero
if group_words:
result = group_words + " " + thousands[group_index] + " " + result
group_index += 1
# Remove extra spaces and return the result
return result.strip()
Case | How to Handle |
---|---|
Input is 0 | Return 'Zero' directly as it's a base case. |
Input is a negative number | Handle negative numbers by checking if num < 0 and returning an error string or throwing an exception, as the problem specifies a non-negative integer. |
Input exceeds the maximum integer value (Integer.MAX_VALUE) | The problem states the input is an integer, but we can handle by checking if the input is larger than Integer.MAX_VALUE and return an error or handle as needed. |
Input is a number in the thousands, millions, or billions range with trailing zeros (e.g., 1000, 1000000, 1000000000) | Ensure correct handling of trailing zeros by properly concatenating thousands, millions, and billions appropriately. |
Input contains only a few significant digits followed by many zeros (e.g., 100000, 20000000) | Correctly handle numbers with multiple trailing zeros by processing digit groups based on magnitude (thousands, millions, etc.). |
Numbers close to the boundaries of thousands, millions, billions (e.g., 999, 999999, 999999999) | Handle transition between different magnitude groups (hundreds to thousands, thousands to millions) correctly. |
Numbers with consecutive zeros (e.g., 1001, 1000010) | Bypass zero values and only consider significant digits during conversion. |
Maximum integer value (2147483647) | Ensure the solution correctly converts the maximum 32-bit integer value to its English word representation. |