Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programming101

Learn everything about programming

Leetcode Integer to English Words problem solution

YASH PAL, 31 July 2024

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

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)

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes