LeetCode – Integer to English Words (Java)

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"

Java Solution

This problem is straightforward, but the corner cases should be considered carefully.

public class Solution {
    HashMap<Integer, String> map = new HashMap<Integer, String>();
    public String numberToWords(int num) {
        StringBuilder sb = new StringBuilder();
            return map.get(0);
        if(num >= 1000000000){
            int extra = num/1000000000;
            sb.append(convert(extra) + " Billion");
            num = num%1000000000;
        if(num >= 1000000){
            int extra = num/1000000;
            sb.append(convert(extra) + " Million");
            num = num%1000000;
        if(num >= 1000){
            int extra = num/1000;
            sb.append(convert(extra) + " Thousand");
            num = num%1000;
        if(num > 0){
        return sb.toString().trim();
    public String convert(int num){
        StringBuilder sb = new StringBuilder();
            int numHundred = num/100;
            sb.append(" " +map.get(numHundred)+ " Hundred");
        if(num > 0){
            if(num>0 && num<=20){
                sb.append(" "+map.get(num));
                int numTen = num/10;
                sb.append(" "+map.get(numTen*10));
                int numOne=num%10;
                    sb.append(" " + map.get(numOne));
        return sb.toString();
    public void fillMap(){
        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");
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Iram22

    private static String eval(int val,HashMap map){
    String res="";
    int head=val/1000000000; int tail=val%1000000000;
    res=eval(head,map)+"Billion "+eval(tail,map);
    else if(val>=1000000){
    int head=val/1000000; int tail=val%1000000;
    res=eval(head,map)+"Million "+eval(tail,map);
    else if(val>=1000){
    int head=val/1000; int tail=val%1000;
    res=eval(head,map)+"Thousand "+eval(tail,map);
    else if(val>=100){
    int head=val/100; int tail=val%100;
    res=eval(head,map)+"Hundred "+eval(tail,map);
    else if(val>=10){
    if(map.containsKey(val)) res=map.get(val);
    int head=(val/10)*10; int tail=val%10;
    else if(val!=0){
    return res;