Integer To English Words (Leetcode 273)

Nishant Tanwar 🕉️
4 min readMar 9, 2021

This is a comprehensive walkthrough for the solution to the problem Integer to English Words problem mentioned on Leetcode.

Problem Description

The question is basically to come up with a way to convert any non negative integer to its English words representation.

eg. 123 is written as “One Hundered Twenty Three”

Approach

The approach we are gonna follow is to divide the number in a set of three like we normally do while writing numbers starting from left to right in sequence one, tens, hundred, thousand, ten thousand, hundred thousand, million, ten million, hundred million, billion and so on.

The flow starts from the call to NumberToWords method. We receive an integer called “num” as input to the method.

We return “Zero” string if num is 0.

We start by calculating the number of billions, millions, thousands, and rest. This we do by dividing the num with One billion equivalent in int and storing it in billion variable. The number we receive after doing mod billion on num gives us the million. We calculate million and thousand in similar fashion. After all this we are left with rest of the digits which are less than thousand.

The next step is to call “Three” named function as we have reduced the num to max three digit numbers as max there can be 999Billion, 999million, 999thousand.

The function “Three” segregates the hundredth position digit from the passed num and passes the rest(obtained by calling mod 100 to num) to the function called “Two” if rest is not 0 and appends the result to the hundred.

The function “Two” checks if the digit is single digit if so calls function “One”, if the num is less than 20 calls the function “TwoLessThanTwenty” containing all integers from 11–19 returning their english equivalent. If the number is greater than 19, we split the num by dividing it to obtain tens digit and calling function “Ten” to return english equivalent and passes the rest(obtained by calling mod 10 to num) to the function “One” to return its english equivalent within the range of 1–9 and appending the result and returning it.

Code

public class Solution {
public string One(int num)
{
switch(num)
{
case 1 : return "One";
case 2 : return "Two";
case 3 : return "Three";
case 4 : return "Four";
case 5 : return "Five";
case 6 : return "Six";
case 7 : return "Seven";
case 8 : return "Eight";
case 9 : return "Nine";
}
return "";
}
public string TwoLessThan20(int num)
{
switch(num)
{
case 10 : return "Ten";
case 11 : return "Eleven";
case 12 : return "Twelve";
case 13 : return "Thirteen";
case 14 : return "Fourteen";
case 15 : return "Fifteen";
case 16 : return "Sixteen";
case 17 : return "Seventeen";
case 18 : return "Eighteen";
case 19 : return "Nineteen";
}
return "";
}
public string Ten(int num)
{
switch(num)
{
case 2 : return "Twenty";
case 3 : return "Thirty";
case 4 : return "Forty";
case 5 : return "Fifty";
case 6 : return "Sixty";
case 7 : return "Seventy";
case 8 : return "Eighty";
case 9 : return "Ninety";
}
return "";
}
public string Two(int num)
{
if(num == 0) return "";

else if(num < 10) return One(num);

else if(num < 20) return TwoLessThan20(num);

else
{
int tenner = num / 10;
int rest = num % 10;
if(rest != 0)
{
return Ten(tenner) + " " + One(rest);
}
else
return Ten(tenner);
}
}
public string Three(int num)
{
int hundred = num / 100;
int rest = num % 100;
string res = "";
if((hundred != 0) && (rest != 0))
res = One(hundred) + " Hundred " + Two(rest);
else if ((hundred == 0) && (rest != 0))
res = Two(rest);
else if((hundred != 0) && (rest == 0))
res = One(hundred) + " Hundred";

return res;
}
public string NumberToWords(int num)
{
if(num == 0) return "Zero";

int billion = num / 1000000000;
int million = (num % 1000000000) / 1000000;
int thousand = ((num % 1000000000) % 1000000)/1000;
int rest = ((num % 1000000000) % 1000000) % 1000;

string result = "";
if(billion != 0)
{
result = Three(billion) + " Billion";
}
if(million != 0)
{
if(result != string.Empty)
{
result += " ";
}
result += Three(million) + " Million";
}
if(thousand != 0)
{
if(result != string.Empty)
{
result += " ";
}
result += Three(thousand) + " Thousand";
}
if(rest != 0)
{
if(result != string.Empty)
{
result += " ";
}
result += Three(rest);
}
return result;
}
}

--

--