273. Integer to English Words (Hard)
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
For example,
123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Hint:
- 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)
Solution 1:
version 1: 6ms
class Solution {
string digitToString(int digit) {
switch (digit) {
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";
default: return "";
}
}
string twoDigitsToString(int d1, int d2) {
if (d2 == 0) {
return digitToString(d1);
} else if (d2 == 1) {
switch (d1) {
case 0: return "Ten";
case 1: return "Eleven";
case 2: return "Twelve";
case 3: return "Thirteen";
case 4: return "Fourteen";
case 5: return "Fifteen";
case 6: return "Sixteen";
case 7: return "Seventeen";
case 8: return "Eighteen";
case 9: return "Nineteen";
}
} else {
string tmp;
switch (d2) {
case 2: tmp = "Twenty"; break;
case 3: tmp = "Thirty"; break;
case 4: tmp = "Forty"; break;
case 5: tmp = "Fifty"; break;
case 6: tmp = "Sixty"; break;
case 7: tmp = "Seventy"; break;
case 8: tmp = "Eighty"; break;
case 9: tmp = "Ninety"; break;
}
string dig1String = digitToString(d1);
return (dig1String.size() == 0) ? tmp : tmp + ' ' + dig1String;
}
}
public:
string numberToWords(int num) {
if (num == 0) return "Zero";
int cnt = 0;
string res;
while (num) {
int chunk = num%1000;
if (chunk == 0) {
num /= 1000;
++cnt;
continue;
}
int dig1 = chunk%10; chunk /= 10;
int dig2 = chunk%10; chunk /= 10;
int dig3 = chunk%10;
string tmp = twoDigitsToString(dig1, dig2);
string curChunkString;
if (dig3) curChunkString = digitToString(dig3)+" Hundred"+(tmp.size() ? " "+tmp:"");
else curChunkString = tmp;
string unit;
switch (cnt) {
case 1: unit = " Thousand"; break;
case 2: unit = " Million"; break;
case 3: unit = " Billion"; break;
}
res = curChunkString+unit+(res.size() ? " "+res:"");
num /= 1000;
++cnt;
}
return res;
}
};
version 2: 6ms
class Solution {
string convertHundred(int num) {
vector<string> v1 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
vector<string> v2 = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string res;
int a = num/100, b = num%100, c = num%10;
res = (b < 20) ? v1[b]: v2[b/10]+(c ? " "+v1[c]:"");
if (a > 0) res = v1[a]+" Hundred"+(b ? " "+res:"");
return res;
}
public:
string numberToWords(int num) {
if (num == 0) return "Zero";
string res = convertHundred(num%1000);
vector<string> v = {"Thousand", "Million", "Billion"};
for (int i = 0; i < 4; ++i) {
num /= 1000;
if (num%1000) res = convertHundred(num%1000)+" "+v[i]+(res.size() ? " "+res:"");
}
return res;
}
};