The Indian Engineer

Problem 273 Integer to English Words

Posted on 6 mins

Math String Recursion

Problem Statement

Link - Problem 273

Question

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:

Solution

class Solution {
public:
    string numberToWords(int num) {

        if (num == 0) return "Zero";


        const vector<string> ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
        const vector<string> tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
        const vector<string> thousands = {"", "Thousand", "Million", "Billion"};


        string result = "";
        int groupIndex = 0;


        while (num > 0) {
            if (num % 1000 != 0) {
                string groupResult = "";
                int part = num % 1000;

                if (part >= 100) {
                    groupResult = ones[part / 100] + " Hundred ";
                    part %= 100;
                }

                if (part >= 20) {
                    groupResult += tens[part / 10] + " ";
                    part %= 10;
                }

                if (part > 0) {
                    groupResult += ones[part] + " ";
                }

                groupResult += thousands[groupIndex] + " ";

                result = groupResult + result;
            }

            num /= 1000;
            groupIndex++;
        }

        return result.substr(0, result.find_last_not_of(" ") + 1);
    }
};

Complexity Analysis

| Algorithm                            | Time Complexity | Space Complexity |
| ------------------------------------ | --------------- | ---------------- |
| Iterative number to words conversion | O(log n)        | O(log n)         |

Explanation

Intial Thoughts

To solve this problem, we need to break down the number into smaller groups of three digits. We should start with the ones, tens, and hundreds places, and then move to thousands, millions, and billions. We can use arrays to map numbers to their corresponding word representations. It’s also important to handle special cases like zero and numbers that are less than 20. We should think about how to handle the ‘and’ and ‘hundred’ keywords in the word representation.

Intuitive Analysis

Intuitively, we can approach this problem by thinking of it like writing a check. We start with the largest denomination and work our way down. For example, if we have 1234, we would say ‘one thousand two hundred thirty four’. We can use this same logic to build our word representation. We should also think about how to handle numbers that have multiple denominations, like thousands and hundreds. Breaking down the problem into smaller parts and using arrays to map numbers to words can help simplify the solution. We can also use a loop to process each group of three digits.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Hard difficulty.

Hints

Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.

Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.

There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)


Similar Questions:

Title URL Difficulty
Integer to Roman https://leetcode.com/problems/integer-to-roman Medium