Welcome to Subscribe On Youtube

273. Integer to English Words

Description

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

Solutions

The approach is designed to convert a numerical value into its English words representation, with the maximum limit capped at billions. The process is structured to manage up to four groups of three digits each.

To facilitate this conversion, a structured method to process groups of three digits is essential. The methodology involves:

  1. Preparation:
    • Create an array containing the English words for numbers 1 through 19, as these numbers have unique names that do not follow a repetitive pattern.
    • Prepare another array for the tens place (20, 30, …, 90), as these numbers follow a pattern and are used as bases for constructing other numbers in their range.
  2. Processing a Three-Digit Number (n):
    • The hundreds place is determined by dividing n by 100 (n/100).
    • The last two digits are obtained by calculating n % 100.
    • The tens place is extracted by dividing the last two digits by 10 (n%100/10).
    • The ones place is found by n % 10.
  3. Handling the Last Two Digits:
    • If the value is less than 20, the corresponding English word is directly fetched from the prepared array.
    • If the value is 20 or more, the words for the tens and ones places are retrieved from their respective arrays.
  4. Addressing the Hundreds Place:
    • If there is a non-zero hundreds place, its English word is appended with “Hundred” to indicate the hundreds place.

This helper function is invoked up to four times within the main function to construct each part of the number’s English words representation, inserting “Thousand”, “Million”, and “Billion” as appropriate to denote the groups’ positions.

Final Adjustments:

  • After assembling the full English representation, any trailing spaces are removed to ensure the format is correct.
  • A special check is conducted for an input of 0; in such cases, the function returns “Zero” to accurately reflect the numerical value.

This methodical approach ensures that numbers are accurately and comprehensively translated into their English words representation, taking into account the unique naming conventions of the English language for numbers.

  • class Solution {
        private static Map<Integer, String> map;
    
        static {
            map = new HashMap<>();
            map.put(1, "One");
            map.put(2, "Two");
            map.put(3, "Three");
            map.put(4, "Four");
            map.put(5, "Five");
            map.put(6, "Six");
            map.put(7, "Seven");
            map.put(8, "Eight");
            map.put(9, "Nine");
            map.put(10, "Ten");
            map.put(11, "Eleven");
            map.put(12, "Twelve");
            map.put(13, "Thirteen");
            map.put(14, "Fourteen");
            map.put(15, "Fifteen");
            map.put(16, "Sixteen");
            map.put(17, "Seventeen");
            map.put(18, "Eighteen");
            map.put(19, "Nineteen");
            map.put(20, "Twenty");
            map.put(30, "Thirty");
            map.put(40, "Forty");
            map.put(50, "Fifty");
            map.put(60, "Sixty");
            map.put(70, "Seventy");
            map.put(80, "Eighty");
            map.put(90, "Ninety");
            map.put(100, "Hundred");
            map.put(1000, "Thousand");
            map.put(1000000, "Million");
            map.put(1000000000, "Billion");
        }
    
        public String numberToWords(int num) {
            if (num == 0) {
                return "Zero";
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 1000000000; i >= 1000; i /= 1000) {
                if (num >= i) {
                    sb.append(get3Digits(num / i)).append(' ').append(map.get(i));
                    num %= i;
                }
            }
            if (num > 0) {
                sb.append(get3Digits(num));
            }
            return sb.substring(1);
        }
    
        private String get3Digits(int num) {
            StringBuilder sb = new StringBuilder();
            if (num >= 100) {
                sb.append(' ').append(map.get(num / 100)).append(' ').append(map.get(100));
                num %= 100;
            }
            if (num > 0) {
                if (num < 20 || num % 10 == 0) {
                    sb.append(' ').append(map.get(num));
                } else {
                    sb.append(' ').append(map.get(num / 10 * 10)).append(' ').append(map.get(num % 10));
                }
            }
            return sb.toString();
        }
    }
    
    
  • class Solution:
        def numberToWords(self, num: int) -> str:
            if num == 0:
                return 'Zero'
    
            lt20 = [
                '',
                'One',
                'Two',
                'Three',
                'Four',
                'Five',
                'Six',
                'Seven',
                'Eight',
                'Nine',
                'Ten',
                'Eleven',
                'Twelve',
                'Thirteen',
                'Fourteen',
                'Fifteen',
                'Sixteen',
                'Seventeen',
                'Eighteen',
                'Nineteen',
            ]
            tens = [
                '',
                'Ten',
                'Twenty',
                'Thirty',
                'Forty',
                'Fifty',
                'Sixty',
                'Seventy',
                'Eighty',
                'Ninety',
            ]
            thousands = ['Billion', 'Million', 'Thousand', '']
    
            def transfer(num):
                if num == 0:
                    return ''
                if num < 20:
                    return lt20[num] + ' '
                if num < 100:
                    return tens[num // 10] + ' ' + transfer(num % 10)
                return lt20[num // 100] + ' Hundred ' + transfer(num % 100)
    
            res = []
            i, j = 1000000000, 0
            while i > 0:
                if num // i != 0:
                    res.append(transfer(num // i))
                    res.append(thousands[j])
                    res.append(' ')
                    num %= i
                j += 1
                i //= 1000
            return ''.join(res).strip()
    
    
  • using System.Collections.Generic;
    using System.Linq;
    
    public class Solution {
        private string[] bases = { "Thousand", "Million", "Billion" };
        public string NumberToWords(int num) {
            if (num == 0)
            {
                return "Zero";
            }
            var baseIndex = -1;
            var parts = new List<string>();
            while (num > 0)
            {
                var part = NumberToWordsInternal(num % 1000);
                if (part.Length > 0 && baseIndex >= 0)
                {
                    part = JoinParts(part, bases[baseIndex]);
                }
                parts.Add(part);
                baseIndex++;
                num /= 1000;
            }
            parts.Reverse();
            return JoinParts(parts);
        }
    
        private string JoinParts(IEnumerable<string> parts)
        {
            return string.Join(" ", parts.Where(p => p.Length > 0));
        }
    
        private string JoinParts(params string[] parts)
        {
            return JoinParts((IEnumerable<string>)parts);
        }
    
        private string NumberToWordsInternal(int num)
        {
            switch(num)
            {
                case 0: return "";
                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";
                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";
            }
    
            if (num < 100)
            {
                string part1;
                switch (num/10)
                {
                    case 2: part1 = "Twenty"; break;
                    case 3: part1 = "Thirty"; break;
                    case 4: part1 = "Forty"; break;
                    case 5: part1 = "Fifty"; break;
                    case 6: part1 = "Sixty"; break;
                    case 7: part1 = "Seventy"; break;
                    case 8: part1 = "Eighty"; break;
                    case 9: default: part1 = "Ninety"; break;
                }
                var part2 = NumberToWordsInternal(num % 10);
                return JoinParts(part1, part2);
            }
    
            {
                var part1 = NumberToWordsInternal(num / 100);
                var part2 = NumberToWordsInternal(num % 100);
                return JoinParts(part1, "Hundred", part2);
            }
        }
    }
    
  • function numberToWords(num: number): string {
        if (num === 0) return 'Zero';
    
        // prettier-ignore
        const f = (x: number): string => {
        const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
        const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
        let ans = ''
    
        if (x <= 19) ans = dict1[x] ?? ''
        else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
        else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
        else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
        else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
        else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
    
        return ans.trim()
      }
    
        return f(num);
    }
    
    
  • function numberToWords(num) {
        if (num === 0) return 'Zero';
    
        // prettier-ignore
        const f = (x) => {
        const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
        const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
        let ans = ''
    
        if (x <= 19) ans = dict1[x] ?? ''
        else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
        else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
        else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
        else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
        else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
    
        return ans.trim()
      }
    
        return f(num);
    }
    
    

All Problems

All Solutions