Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Integer to English Words problem solution

YASH PAL, 31 July 202421 January 2026

In this Leetcode Integer to English Words problem solution we need to Convert a non-negative integer num to its English words representation.

Leetcode Integer to English Words problem solution

Leetcode Integer to English Words problem solution in Python.

class Solution:
    def numberToWords(self, num: int) -> str:
        self.lessThan20 = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
        self.tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
        self.thousands = ["", "Thousand", "Million", "Billion"];
        if num == 0 :
            return "Zero"
        res = ''
        i = 0
        while num>0:
            if num%1000 != 0:
                res = self.helper(num%1000) + self.thousands[i] + " " + res
            num //= 1000
            i += 1
        return res.strip()
    
    def helper(self, num):
        if num == 0:
            return ""
        if num < 20:
            return self.lessThan20[num] + " "
        if num < 100:
            return self.tens[num//10] + " " + self.helper(num%10)
        if num < 1000:
            return self.lessThan20[num//100] + " Hundred " + self.helper(num%100)

Integer to English Words problem solution in Java.

class Solution {
    static HashMap<Integer,String> hm;
    public static void fill() {
        hm = new HashMap<Integer, String>();
        hm.put(1_000_000_000,"Billion");
        hm.put(1_000_000,"Million");
        hm.put(1_000,"Thousand");
        hm.put(100, "Hundred");
    }
    static String ten[] = {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve",
            "Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    static String two[] = {"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    public static String numberToWords(int n) {
        if(n<1)
            return "Zero";
        fill();
        int base = 1_000_000_000;
        StringBuilder sb = new StringBuilder();
        while(n>0&&base>1) {
            if(n>=base) {
                int val = n/base;
                sb.append(get(val));
                sb.append(hm.get(base)+" ");
                n%=base;
            }
            base/=1000;
        }
        sb.append(get(n));
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }
    public static String get(int n) {
        StringBuilder sb = new StringBuilder();
        while(n>0) {
            if(n>=100) {
                int val=n/100;
                sb.append(ten[val]+" ");
                sb.append(hm.get(100)+" ");
                n%=100;
            }
            else if(n>=20) {
                int val = n/10;
                sb.append(two[val]+" ");
                n%=10;
            }
            else if(n<20) {
                sb.append(ten[n]+" ");
                n=0;
            }
        }
        return sb.toString();
    }
}

Problem solution in C++.

class Solution {
public:
    string hundredStr(int num){
        vector<string> arr1={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
        "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
        vector<string> arr2={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
        string ret;
        ret=num%100<20?arr1[num%100]:arr2[(num%100)/10]+(num%10?" "+arr1[num%10]:"");
        if(num>99)ret=arr1[num/100]+" Hundred"+(num%100?" "+ret:"");
        return ret;
    }
    string numberToWords(int num) {
        string ret;
        vector<string> strarr={"Thousand","Million","Billion"};
        ret=hundredStr(num%1000);
        for(int i=0;i<3;i++){
            num/=1000;
            ret=num%1000?hundredStr(num%1000)+" "+strarr[i]+" "+ ret:ret;
        }
        while(ret.back()==' ')ret.pop_back();
        return ret.empty()?"Zero":ret;
    }
};

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes