Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd0478c

Browse files
Integer to english words
1 parent 9d5b0ef commit fd0478c

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

‎0273_integerToEnglishWords.js‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const zeroToNine = {
2+
0: 'Zero',
3+
1: 'One',
4+
2: 'Two',
5+
3: 'Three',
6+
4: 'Four',
7+
5: 'Five',
8+
6: 'Six',
9+
7: 'Seven',
10+
8: 'Eight',
11+
9: 'Nine',
12+
};
13+
14+
const tenToNineteen = {
15+
10: 'Ten',
16+
11: 'Eleven',
17+
12: 'Twelve',
18+
13: 'Thirteen',
19+
14: 'Fourteen',
20+
15: 'Fifteen',
21+
16: 'Sixteen',
22+
17: 'Seventeen',
23+
18: 'Eighteen',
24+
19: 'Nineteen',
25+
};
26+
27+
const twentyToNinety = {
28+
2: 'Twenty',
29+
3: 'Thirty',
30+
4: 'Forty',
31+
5: 'Fifty',
32+
6: 'Sixty',
33+
7: 'Seventy',
34+
8: 'Eighty',
35+
9: 'Ninety',
36+
};
37+
38+
const BILLION = 1000000000;
39+
const MILLION = 1000000;
40+
const THOUSAND = 1000;
41+
const HUNDRED = 100;
42+
const TEN = 10;
43+
44+
const three = num => {
45+
const solution = [];
46+
47+
const hundreds = parseInt(num / HUNDRED);
48+
49+
if (hundreds >= 1) {
50+
solution.push(zeroToNine[hundreds] + ' Hundred');
51+
num = num - hundreds * HUNDRED;
52+
}
53+
54+
if (num && num < 20) {
55+
solution.push(zeroToNine[num] || tenToNineteen[num]);
56+
} else {
57+
const tens = parseInt(num / TEN);
58+
if (tens) solution.push(twentyToNinety[tens]);
59+
num = num - tens * TEN;
60+
if (num) solution.push(zeroToNine[num]);
61+
}
62+
63+
return solution.join(' ');
64+
};
65+
66+
/**
67+
* @param {number} num Number to be converted to words.
68+
* @return {string} Result of converting input number to words.
69+
* @summary Integer to English Words {@link https://leetcode.com/problems/integer-to-english-words/}
70+
* @description Convert input string to english word representation.
71+
* Space O(1) - one resulting string.
72+
* Time O(n) - grows in line with size of the input.
73+
*/
74+
const numberToWords = num => {
75+
if (num < 19) return zeroToNine[num] || tenToNineteen[num];
76+
77+
const billionCount = num < BILLION ? 0 : parseInt(num / BILLION);
78+
const millionCount = num < MILLION ? 0 : parseInt((num - billionCount * BILLION) / MILLION);
79+
const thousandCount =
80+
num < THOUSAND ? 0 : parseInt((num - billionCount * BILLION - millionCount * MILLION) / THOUSAND);
81+
const rest = parseInt(num - billionCount * BILLION - millionCount * MILLION - thousandCount * THOUSAND);
82+
83+
const solution = [];
84+
85+
if (billionCount >= 1) solution.push(three(billionCount) + ' Billion');
86+
if (millionCount >= 1) solution.push(three(millionCount) + ' Million');
87+
if (thousandCount >= 1) solution.push(three(thousandCount) + ' Thousand');
88+
if (rest >= 1) solution.push(three(rest));
89+
90+
return solution.join(' ');
91+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /