Integer to English Words - Complete Solution Guide
Integer to English Words is LeetCode problem 273, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 <= 2 31 - 1
Detailed Explanation
The problem requires converting a non-negative integer into its English word representation. The input is an integer `num` between 0 and 2<sup>31</sup> - 1. The output is a string representing the English words for the given number. For example, 123 should be converted to "One Hundred Twenty Three".
Solution Approach
The solution uses a divide-and-conquer approach. It first handles the base case where the number is 0. Then, it defines lookup tables for numbers less than 20, tens, and thousands. A helper function `to_words_helper` converts a number less than 1000 into words. The main function iterates through the input number in chunks of 1000, converting each chunk to words using the helper function and appending the appropriate scale (Thousand, Million, Billion). Finally, it joins the words with spaces to form the final string.
Step-by-Step Algorithm
- Step 1: Handle the base case: If the number is 0, return "Zero".
- Step 2: Define lookup tables: Create arrays or lists containing English words for numbers less than 20, tens, and powers of 1000.
- Step 3: Implement a helper function `to_words_helper(n)`: This function converts a number `n` less than 1000 into words. It handles numbers less than 20 directly. For numbers between 20 and 99, it uses the tens array and the ones array. For numbers between 100 and 999, it adds the "Hundred" suffix and recursively calls the helper for the remaining two digits.
- Step 4: Iterate through the input number in chunks of 1000: Use a `while` loop to process the input number. In each iteration, extract the last three digits (`num % 1000`).
- Step 5: Convert the chunk to words using `to_words_helper`: If the chunk is non-zero, convert it to words and append the appropriate scale (Thousand, Million, Billion) based on the current iteration.
- Step 6: Prepend the chunk words to the result: Add the chunk words (and the scale) to the beginning of the result list.
- Step 7: Update the number and scale index: Divide the number by 1000 (`num //= 1000`) and increment the scale index.
- Step 8: Join the words in the result list with spaces: Create the final string by joining the words in the result list with spaces.
Key Insights
- Insight 1: The problem can be broken down into smaller subproblems by processing the number in chunks of three digits (hundreds, tens, and ones).
- Insight 2: Using lookup tables (arrays or lists) to store the English words for numbers below 20, tens (20, 30, ..., 90), and powers of 1000 (Thousand, Million, Billion) simplifies the conversion process.
- Insight 3: A recursive or iterative helper function can be used to convert a three-digit number into its English word representation. Handling the edge case of zero is also essential.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(1)
Topics
This problem involves: Math, String, Recursion.
Companies
Asked at: Attentive, Avalara, Block, Delhivery, Gusto, Nordstrom, Palantir Technologies, Roblox, Snowflake, Warnermedia, Yext, Zoho, eBay.