class Solution {
// Arrays for single digits, tens, and numbers between 10 and 19
private static final String[] ones = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private static final String[] tens = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private static final String[] thousands = { "", "Thousand", "Million", "Billion", "Trillion"};
// Helper function to convert numbers less than 1000 into words
private String convertLessThan1000(int num) {
if (num == 0) {
return "";
}
String result = "";
if (num >= 100) {
result += ones[num / 100] + " Hundred ";
num %= 100;
}
if (num >= 20) {
result += tens[num / 10] + " ";
num %= 10;
}
if (num > 0) {
result += ones[num] + " ";
}
return result.trim();
}
// Main function to convert the number to words
public String convertToWords(int n) {
if (n == 0) {
return "Zero";
}
String result = "";
int i = 0;
while (n > 0) {
if (n % 1000 != 0) {
result = convertLessThan1000(n % 1000) + " " + thousands[i] + " " + result;
}
n /= 1000;
i++;
}
// Remove trailing space
return result.trim();
}
}
Let's perform a dry run of the code you provided, which converts an integer to its English words representation, for better understanding.
This code contains:
-
Three arrays (
ones
,tens
,thousands
) that help map numbers to their English equivalents.ones[]
holds English words for numbers 0 to 19.tens[]
holds English words for multiples of Ten from 20 onward (20, 30, 40, ..., 90).thousands[]
holds English words for large place values like Thousand, Million, Billion, etc.
-
The helper function
convertLessThan1000(int num)
is responsible for converting any number less than 1000 to English words. -
The
convertToWords(int n)
method processes the number, breaking it down into groups of three digits (representing thousands, millions, etc.) and converts each group using the helper function.
Let's dry-run the function with different example inputs.
Step-by-Step Breakdown:
-
The
convertToWords()
function is called withn = 12345
. -
Initialization:
result = ""
(This will hold the final result.)i = 0
(This tracks the thousands, millions, etc. placeholders.)
-
First iteration (
n = 12345
):n % 1000 = 345
, which is not 0, so we callconvertLessThan1000(345)
.- convertLessThan1000(345):
345 >= 100
, so it adds "Three Hundred ".- Then,
345 % 100 = 45
. 45 >= 20
, so it adds "Forty ".- Finally,
45 % 10 = 5
, so it adds "Five ". - The result of
convertLessThan1000(345)
is"Three Hundred Forty Five"
.
- convertLessThan1000(345):
thousands[i] = ""
(empty string, because this is for the "ones" place).- The result for this iteration is:
"Three Hundred Forty Five "
.
-
Update:
n /= 1000
, so nown = 12
.i++
, so nowi = 1
.
-
Second iteration (
n = 12
):n % 1000 = 12
, which is not 0, so we callconvertLessThan1000(12)
.- convertLessThan1000(12):
- Since
12 <= 19
, it directly adds "Twelve".
- Since
thousands[i] = "Thousand"
.- The result for this iteration is:
"Twelve Thousand "
.
- convertLessThan1000(12):
-
Update:
n /= 1000
, so nown = 0
.i++
, so nowi = 2
.
-
Termination:
- Since
n = 0
, the while loop terminates.
- Since
-
Final result:
- Combine the results from both iterations:
"Three Hundred Forty Five Twelve Thousand "
. - Remove the trailing spaces:
"Twelve Thousand Three Hundred Forty Five"
.
- Combine the results from both iterations:
Output: "Twelve Thousand Three Hundred Forty Five"
.
Step-by-Step Breakdown:
-
The
convertToWords()
function is called withn = 1001
. -
Initialization:
result = ""
i = 0
-
First iteration (
n = 1001
):n % 1000 = 1
, which is not 0, so we callconvertLessThan1000(1)
.- convertLessThan1000(1):
- Since
1 <= 19
, it directly adds "One".
- Since
thousands[i] = ""
(empty string, because this is for the "ones" place).- The result for this iteration is:
"One "
.
- convertLessThan1000(1):
-
Update:
n /= 1000
, so nown = 1
.i++
, so nowi = 1
.
-
Second iteration (
n = 1
):n % 1000 = 1
, which is not 0, so we callconvertLessThan1000(1)
.- convertLessThan1000(1):
- Since
1 <= 19
, it directly adds "One".
- Since
thousands[i] = "Thousand"
.- The result for this iteration is:
"One Thousand "
.
- convertLessThan1000(1):
-
Update:
n /= 1000
, so nown = 0
.i++
, so nowi = 2
.
-
Termination:
- Since
n = 0
, the while loop terminates.
- Since
-
Final result:
- Combine the results from both iterations:
"One One Thousand "
. - Remove the trailing spaces:
"One Thousand"
.
- Combine the results from both iterations:
Output: "One Thousand"
.
Step-by-Step Breakdown:
-
The
convertToWords()
function is called withn = 0
. -
Special Case:
- Since
n == 0
, the function immediately returns"Zero"
.
- Since
Output: "Zero"
.
Step-by-Step Breakdown:
-
The
convertToWords()
function is called withn = 99999
. -
Initialization:
result = ""
i = 0
-
First iteration (
n = 99999
):n % 1000 = 999
, which is not 0, so we callconvertLessThan1000(999)
.- convertLessThan1000(999):
999 >= 100
, so it adds "Nine Hundred ".- Then,
999 % 100 = 99
. 99 >= 20
, so it adds "Ninety ".- Finally,
99 % 10 = 9
, so it adds "Nine ". - The result of
convertLessThan1000(999)
is"Nine Hundred Ninety Nine"
.
thousands[i] = ""
(empty string).- The result for this iteration is:
"Nine Hundred Ninety Nine "
.
- convertLessThan1000(999):
-
Update:
n /= 1000
, so nown = 99
.i++
, so nowi = 1
.
-
Second iteration (
n = 99
):n % 1000 = 99
, which is not 0, so we callconvertLessThan1000(99)
.- convertLessThan1000(99):
99 >= 20
, so it adds "Ninety ".- Then,
99 % 10 = 9
, so it adds "Nine ". - The result of
convertLessThan1000(99)
is"Ninety Nine"
.
thousands[i] = "Thousand"
.- The result for this iteration is:
"Ninety Nine Thousand "
.
- convertLessThan1000(99):
-
Update:
n /= 1000
, so nown = 0
.i++
, so nowi = 2
.
-
Termination:
- Since
n = 0
, the while loop terminates.
- Since
-
Final result:
- Combine the results from both iterations:
"Nine Hundred Ninety Nine Ninety Nine Thousand "
. - Remove the trailing spaces:
"Ninety Nine Thousand Nine Hundred Ninety Nine"
.
- Combine the results from both iterations:
Output: "Ninety Nine Thousand Nine Hundred Ninety Nine"
.
This code successfully converts a number into its English words representation, breaking down the number into groups of three digits and converting them using predefined mappings for numbers less than 1000, then combining the results with appropriate place values (Thousand, Million, etc.). The logic handles edge cases like 0 and trailing spaces effectively.