# Java Solution with one lookup table

• This solution uses just one lookup table and easy to understand:

``````public class Solution {
public String numberToWords(int num) {
HashMap<Integer, String> map = new HashMap<Integer, String> ();
map.put(0, "Zero");
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
map.put(4, "Four");
map.put(5, "Five");
map.put(6, "Six");
map.put(7, "Seven");
map.put(8, "Eight");
map.put(9, "Nine");
map.put(10, "Ten");
map.put(11, "Eleven");
map.put(12, "Twelve");
map.put(13, "Thirteen");
map.put(14, "Fourteen");
map.put(15, "Fifteen");
map.put(16, "Sixteen");
map.put(17, "Seventeen");
map.put(18, "Eighteen");
map.put(19, "Nineteen");
map.put(20, "Twenty");
map.put(30, "Thirty");
map.put(40, "Forty");
map.put(50, "Fifty");
map.put(60, "Sixty");
map.put(70, "Seventy");
map.put(80, "Eighty");
map.put(90, "Ninety");
map.put(100, "Hundred");
map.put(1000, "Thousand");
map.put(1000000, "Million");
map.put(1000000000, "Billion");

int[] steps= new int[]{1000000000, 1000000, 1000, 1};
StringBuilder sb = new StringBuilder();
for(int step: steps) {
if (num/step>0)  {
sb.append(helper(num/step, map));
if (step>1) {
sb.append(map.get(step)).append(" ");
}
}
num = num%step;
}
String result = sb.toString().trim();
if (result.length()==0) {
return "Zero";
}
else {
return result;
}
}
String helper(int n, Map<Integer,String>map) {
StringBuilder sb = new StringBuilder();
if (n/100>0) {
sb.append(map.get(n/100)).append(" ");
sb.append(map.get(100)).append(" ");
n = n%100;
}
if (n/20>0){
sb.append(map.get(n-n%10)).append(" ");
n = n%10;
}
if (n>0) {
sb.append(map.get(n)).append(" ");
}
return sb.toString();
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.