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
This problem requires converting a non-negative integer into its English words representation. The solutions presented use a recursive approach combined with string manipulation and pre-defined arrays for efficiency.
Core Idea:
The solutions break down the problem into smaller, manageable parts. They handle the conversion of numbers in three-digit chunks (hundreds, tens, and ones) and then concatenate these chunks with appropriate thousands separators (thousand, million, billion). Recursion simplifies the handling of the three-digit chunks.
Approach:
Pre-defined Arrays: Both solutions use pre-defined arrays (lt20
, tens
, thousands
) to store English word representations of numbers from 0 to 19, tens multiples (20, 30,...), and thousands separators. This significantly speeds up the conversion process.
Recursive Helper Function (transfer or NumberToWordsInternal): A recursive helper function is crucial for handling three-digit numbers. This function recursively breaks down the number into hundreds, tens, and ones, converts them to words using the pre-defined arrays, and concatenates them.
Main Function (numberToWords): The main numberToWords
function handles the overall conversion. It iterates through the number in chunks of three digits (from billions to ones). For each chunk, it calls the helper function transfer
or NumberToWordsInternal
to get the English representation and then appends the appropriate thousands separator ("Billion", "Million", "Thousand").
Code Explanation (Python Solution 1):
The Python solution demonstrates this approach clearly.
lt20
, tens
, thousands
: Pre-defined arrays as described above.transfer(num)
: This recursive function handles the three-digit conversion. It checks the range of the number and uses the appropriate array to get the English words.numberToWords(num)
: The main function iterates through billions, millions, thousands, and hundreds places, calling transfer
for each chunk and appending the appropriate thousands separator.Time and Space Complexity Analysis:
Time Complexity: O(log1000N), where N is the input number. The number of iterations in the main function is proportional to the number of three-digit chunks in the input number (which is logarithmic with base 1000). The helper function also has a time complexity proportional to the number of digits in the three-digit chunk (which is constant). Therefore, the dominant factor is the logarithmic number of iterations.
Space Complexity: O(log1000N) in the worst case due to the recursive calls in the helper function. The depth of recursion is proportional to the number of three-digit chunks. The space used by the arrays is constant. Therefore, the space complexity is determined by the recursion depth.
Other Language Solutions:
The Java, C#, TypeScript, and JavaScript solutions follow a similar logic and structure, using pre-defined arrays and recursion to achieve the conversion. The minor differences lie in the syntax and data structure implementations of each language.
Improvements:
The solutions could be slightly optimized by using a more efficient string concatenation method (like StringBuilder
in Java) to reduce the overhead of string concatenation within loops. However, the primary efficiency gains come from the clever use of pre-defined arrays and recursion.