Integer to English - Amazon Top Interview Questions


Problem Statement :


Given a non-negative integer num, convert it to an English number word as a string. num will be less than one trillion.

Constraints

0 ≤ num < 10 ** 12

Example 1

Input

num = 523

Output

"Five Hundred Twenty Three"

Example 2

Input

num = 823418

Output

"Eight Hundred Twenty Three Thousand Four Hundred Eighteen"

Example 3

Input

num = 700

Output

"Seven Hundred"



Solution :



title-img




                        Solution in C++ :

vector<pair<string, int>> tbl{{"Billion", 1000000000},
                              {"Million", 1000000},
                              {"Thousand", 1000},
                              {"Hundred", 100},
                              {"Ninety", 90},
                              {"Eighty", 80},
                              {"Seventy", 70},
                              {"Sixty", 60},
                              {"Fifty", 50},
                              {"Forty", 40},
                              {"Thirty", 30},
                              {"Twenty", 20},
                              {"Nineteen", 19},
                              {"Eighteen", 18},
                              {"Seventeen", 17},
                              {"Sixteen", 16},
                              {"Fifteen", 15},
                              {"Fourteen", 14},
                              {"Thirteen", 13},
                              {"Twelve", 12},
                              {"Eleven", 11},
                              {"Ten", 10},
                              {"Nine", 9},
                              {"Eight", 8},
                              {"Seven", 7},
                              {"Six", 6},
                              {"Five", 5},
                              {"Four", 4},
                              {"Three", 3},
                              {"Two", 2},
                              {"One", 1}};
string solve(int num) {
    if (num == 0) return "Zero";
    string ans;
    for (auto& p : tbl) {
        if (p.second <= num) {
            if (p.second >= 100) {
                ans = solve(num / p.second) + " " + p.first;
                if (num > (num / p.second) * p.second)
                    ans += " " + solve(num - (num / p.second) * p.second);
            } else {
                ans = p.first + (num > p.second ? " " + solve(num - p.second) : "");
            }
            break;
        }
    }
    return ans;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public String solve(int num) {
        if (num == 0) {
            return "Zero";
        }
        StringBuilder sb = new StringBuilder();
        if (num >= 1000000000) {
            oneToNineHundreadNinetyNine(sb, num / 1000000000).append("Billion ");
            num %= 1000000000;
        }
        if (num >= 1000000) {
            oneToNineHundreadNinetyNine(sb, num / 1000000).append("Million ");
            num %= 1000000;
        }
        if (num >= 1000) {
            oneToNineHundreadNinetyNine(sb, num / 1000).append("Thousand ");
            num %= 1000;
        }
        return oneToNineHundreadNinetyNine(sb, num).toString().trim();
    }

    private StringBuilder oneToNineHundreadNinetyNine(StringBuilder sb, int num) {
        if (num >= 100) {
            sb.append(oneToNineteen(num / 100)).append("Hundred ");
            num %= 100;
        }
        if (num >= 20) {
            sb.append(twentyToNinety(num));
            num %= 10;
        }
        sb.append(oneToNineteen(num));
        return sb;
    }

    private String oneToNineteen(int num) {
        switch (num % 20) {
            default:
                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 ";
        }
    }

    private String twentyToNinety(int num) {
        switch (num / 10 % 10) {
            default:
                return "";
            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 ";
        }
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, num):
        if not num:
            return "Zero"
        _0_to_19 = [
            "Zero",
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine",
            "Ten",
            "Eleven",
            "Twelve",
            "Thirteen",
            "Fourteen",
            "Fifteen",
            "Sixteen",
            "Seventeen",
            "Eighteen",
            "Nineteen",
        ]
        _0_to_90_by_10 = [
            "",
            "",
            "Twenty",
            "Thirty",
            "Forty",
            "Fifty",
            "Sixty",
            "Seventy",
            "Eighty",
            "Ninety",
        ]
        _0_to_tri_by_1000 = [
            "",
            "Thousand",
            "Million",
            "Billion",
            "Trillion",
        ]

        def say_19(x):
            return x and [_0_to_19[x]] or []

        def say_99(x):
            return x >= 20 and [_0_to_90_by_10[x // 10]] + say_19(x % 10) or say_19(x)

        def say_999(x, suf):
            return (
                (x >= 100 and [_0_to_19[x // 100]] + ["Hundred"] or [])
                + say_99(x % 100)
                + (x and suf and [suf] or [])
            )

        def say(x, p):
            return x and say(x // 1000, p + 1) + say_999(x % 1000, _0_to_tri_by_1000[p]) or []

        return " ".join(say(num, 0))
                    


View More Similar Problems

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →