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

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →