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 :
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
Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →Direct Connections
Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do
View Solution →