public class Solution {
public String numberToWords(int num) {
if (num == 0) return "Zero";
String[] lessThan20 = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] tyWord = new String[] {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
String[] dexWord = new String[] {"Billion", "Million", "Thousand", "Hundred"};
int[] radix = new int[] {1000000000, 1000000, 1000, 100};
StringBuffer sb = new StringBuffer();
int count = 0;
for (int i = 0; i < radix.length; i++) {
count = num / radix[i];
num %= radix[i];
if (count == 0) continue;
sb.append(numberToWords(count) + " ");
sb.append(dexWord[i] + " ");
}
if (num < 20) {
count = num % 20;
sb.append(lessThan20[count]);
} else {
count = num / 10;
sb.append(tyWord[count] + " ");
count = num % 10;
sb.append(lessThan20[count]);
}
return sb.toString().trim();
}
}