0% completed
Problem Statement
Given a positive integer num
, return the English word
representation of num
.
Examples
-
Example 1:
- Input: num =
12345
- Expected Output:
"Twelve Thousand Three Hundred Forty Five"
- Justification: The number 12345 is broken down into "Twelve Thousand" for the first two digits, followed by "Three Hundred" for the next digit, and "Forty Five" for the last two digits.
- Input: num =
-
Example 2:
- Input: num =
1001
- Expected Output:
"One Thousand One"
- Justification: The number 1001 translates to "One Thousand" for the first digit and "One" for the last digit, skipping the hundreds and tens place because they are zeros.
- Input: num =
-
Example 3:
- Input: num =
2000000
- Expected Output:
"Two Million"
- Justification: The number 2000000 is simply "Two Million" as there are no other numbers to represent after the word "Million".
- Input: num =
Solution
To solve this problem, the approach involves breaking down the given number into manageable chunks, each representing a part of the number in words. This method is effective because it mirrors how humans naturally interpret numbers. By dividing the number into segments such as billions, millions, thousands, and then the last three digits, we can systematically convert each segment into words using predefined mappings for numbers 1-19, tens (20, 30, ..., 90), and the significant place values (thousand, million, billion). This approach ensures that we can handle any non-negative integer input systematically and efficiently.
The algorithm's effectiveness lies in its simplicity and scalability. By treating the number in segments, we can reuse the same logic for each segment, making the code more maintainable and easier to understand. This method also scales well for large numbers, as it applies the same logic regardless of the number's size, ensuring consistency in the output.
Step-by-Step Algorithm
-
Initialization: Define three arrays or lists for mapping numbers to words. These include:
lessThan20
for numbers 1 to 19.tens
for multiples of ten from 20 to 90.thousands
for the place values thousand, million, and billion.
-
Check for Zero: If the input number is 0, immediately return the word "Zero".
-
Conversion Process:
- Initialize an empty string
words
to hold the final words representation. - Initialize a counter
i
to keep track of the current place value (thousands, millions, etc.). - While the input number is greater than 0, perform the following steps:
- If the last three digits of the current number segment (num % 1000) are not zero, convert this segment to words using a helper function and append the appropriate place value from the
thousands
array. - Divide the number by 1000 to move to the next segment.
- Increment the place value counter
i
.
- If the last three digits of the current number segment (num % 1000) are not zero, convert this segment to words using a helper function and append the appropriate place value from the
- Initialize an empty string
-
Helper Function:
- This function takes a number less than 1000 and converts it into words.
- If the number is less than 20, return the corresponding word from
lessThan20
. - If the number is between 20 and 99, return the word for the tens place from
tens
and recursively call the helper function for the remainder (units place). - If the number is between 100 and 999, return the word for the hundreds place from
lessThan20
, the word "Hundred", and recursively call the helper function for the remainder (less than 100).
-
Finalization:
- Trim any leading or trailing spaces from the
words
string. - Return the
words
string as the final English words representation of the input number.
- Trim any leading or trailing spaces from the
Algorithm Walkthrough
Let's consider the input: num = "12345"
-
Initialization: The mappings for numbers to words are defined in arrays/lists as per the algorithm.
-
Check for Zero: The input number is 12345, which is not zero, so we proceed.
-
Conversion Process:
- The input number 12345 is greater than 0, so we start the conversion.
- First Iteration:
- The last three digits are 345. Next, we convert 345 to words.
- We call the helper function with 345.
- The number is greater than 100, so we get "Three Hundred " + helper(45).
- For 45, it falls in the less than 100 category. So, we get "Forty " + helper(45%10).
- For 5, it falls in the less than 20 category. So, we get "Five".
- The final return for this segment is "Three Hundred Forty Five".
- We append "" (empty string) after converting 345 to words because
i = 0
(first segment after the units). - The words string remains same "Three Hundred Forty Five ".
- We divide the number by 1000, updating it to 12.
- Second Iteration:
- Now, the number is 12.
- Since 12 % 1000 = 12 is not zero, we convert 12 to words using the helper function.
- 12 is less than 20, so we directly get "Twelve" from
lessThan20
.
- 12 is less than 20, so we directly get "Twelve" from
- Prepand
words
string withthousands[1]
andhelper(12)
. - The words string is updated to "Twelve Thousand Three Hundred Forty Five ".
- The loop ends as the number is now 0.
-
Finalization:
- Trim the final words string to remove any trailing space.
- The final output is "Twelve Thousand Three Hundred Forty Five".
Code
Complexity Analysis
Time Complexity
The algorithm has a linear time complexity relative to the number of digits in the input number. In the worst case, it processes each group of three digits once, leading to a time complexity of O(n/3) = O(n), where n is the number of digits.
Space Complexity
The space complexity is O(1) for the storage of mappings (lessThan20, tens, thousands), which does not scale with the input size. However, the recursive calls in the helper function use stack space, but since the depth is limited to a maximum of 3 (for numbers less than 1000), it can also be considered O(1).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible