Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
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 *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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