Skip to main content

0273 Integer to English Words

In this tutorial, we will solve the Integer to English Words problem. We will provide the implementation of the solution in JavaScript, Python, Java, C++, and more.

Problem Description​

Convert a non-negative integer num to its English words representation.

Examples​

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−10 \leq \text{num} \leq 2^31 - 1

Solution for Two Sum Problem​

Intuition and Approach​

Understanding the structure and pattern of English numbers is essential to translating numbers into their representation in language. By taking into account the following, we may divide the issue into manageable chunks:

  1. We build a list for the unique names of numbers 1 through 19, which do not follow any particular pattern (lt20).
  2. We list the tens from 20 to 90 because they have unique names as well.
  3. Generally speaking, complex numbers are sums of smaller numbers. For instance, 345 can be interpreted as "Three Hundred Forty-Five," which is the result of adding the smaller number "Forty-Five" to the word "Three Hundred".
  4. The way that numbers higher than 99 are named in English is by adding a scale word, such as "Thousand," "Million," or "Billion," and then saying the name of the next number that comes after.

In light of this, we tackle the issue by developing a recursive function transfer that is capable of handling values smaller than a thousand. Subsequently, we employ an iterative process to apply this function to segments of the input number, gradually decreasing from billions to units, attaching suitable scale terms, and concatenating these segments to generate the complete representation of English words.

We keep an array thousands, which is made up of scale words divided by a factor of ten, and an adjusted counter to work on fractions of the number, in order to track units of thousand. The number can be divided into segments that can be handled by transfer before the matching scale term (if any) is attached by using division and modulo operations.

The method begins with the largest unit that can be used, billions, then gradually removes portions of the number until the whole amount has been converted. After that, we combine the created segments and eliminate any remaining spaces to obtain the final representation of the English words.

Code in Different Languages​

const map = new Map();
map.set(1, "One");
map.set(2, "Two");
map.set(3, "Three");
map.set(4, "Four");
map.set(5, "Five");
map.set(6, "Six");
map.set(7, "Seven");
map.set(8, "Eight");
map.set(9, "Nine");
map.set(10, "Ten");
map.set(11, "Eleven");
map.set(12, "Twelve");
map.set(13, "Thirteen");
map.set(14, "Fourteen");
map.set(15, "Fifteen");
map.set(16, "Sixteen");
map.set(17, "Seventeen");
map.set(18, "Eighteen");
map.set(19, "Nineteen");
map.set(20, "Twenty");
map.set(30, "Thirty");
map.set(40, "Forty");
map.set(50, "Fifty");
map.set(60, "Sixty");
map.set(70, "Seventy");
map.set(80, "Eighty");
map.set(90, "Ninety");
map.set(100, "Hundred");
map.set(1000, "Thousand");
map.set(1000000, "Million");
map.set(1000000000, "Billion");

function numberToWords(num) {
if (num === 0) {
return "Zero";
}
let result = "";
for (let i = 1000000000; i >= 1000; i /= 1000) {
if (num >= i) {
result += get3Digits(Math.floor(num / i)) + " " + map.get(i) + " ";
num %= i;
}
}
if (num > 0) {
result += get3Digits(num);
}
return result.trim();
}

function get3Digits(num) {
let result = "";
if (num >= 100) {
result += " " + map.get(Math.floor(num / 100)) + " " + map.get(100);
num %= 100;
}
if (num > 0) {
if (num < 20 || num % 10 === 0) {
result += " " + map.get(num);
} else {
result +=
" " + map.get(Math.floor(num / 10) * 10) + " " + map.get(num % 10);
}
}
return result.trim();
}

Complexity Analysis​

  • Time Complexity: O(n)O(n)
  • Space Complexity: O(n)O(n)
  • The time complexity is O(n)O(n) where n is the number of digits in the input number since each digit or group of digits is processed once
  • The space complexity is O(n)O(n) due to the storage required for the word representation of the number

Authors:

Loading...