Programming & Development / April 10, 2025

LeetCode 273: Integer to English Words โ€“ Convert Numbers into Spoken Format

LeetCode 273 Integer to English Words number to words string conversion recursion number formatting Python hard problem large numbers edge cases interview question

Introduction

LeetCode 273: Integer to English Words is a classic string formatting and recursion problem. It asks you to convert an integer into its spoken English equivalent. Think of it like building your own version of num2words().

This is a popular system design building block, commonly used in:

  • Cheque writing software
  • Accounting tools
  • Assistive tech (text-to-speech)
  • Natural Language Processing systems

Problem Statement

Convert a non-negative integer num into its English words representation.
You must return the result as a string without extra spaces.

Examples

python

Input: num = 123
Output: "One Hundred Twenty Three"

Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"

Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

๐Ÿง  Strategy & Breakdown

We break the number into chunks of three digits (hundreds, thousands, millions, billions), and convert each using recursion or mapping.

๐Ÿ”ข Step 1: Define Core Words

We need three groups of mappings:

python

<20       = ['One', 'Two', ..., 'Nineteen']
tens      = ['Twenty', 'Thirty', ..., 'Ninety']
thousands = ['', 'Thousand', 'Million', 'Billion']

๐Ÿ” Step 2: Convert 3-Digit Chunk

Create a helper function to convert a number less than 1000:

  • For example:
  • 123 โ†’ "One Hundred Twenty Three"
  • 508 โ†’ "Five Hundred Eight"

๐Ÿ” Step 3: Process Chunks From Right to Left

  • For 1,234,567:
  • 567 โ†’ "Five Hundred Sixty Seven"
  • 234 โ†’ "Two Hundred Thirty Four Thousand"
  • 1 โ†’ "One Million"

Join them together in reverse order.

โœ… Python Code

python

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

๐Ÿงช Dry Run

Input: num = 1234567

Chunks:

  • 567 โ†’ "Five Hundred Sixty Seven"
  • 234 โ†’ "Two Hundred Thirty Four Thousand"
  • 1 โ†’ "One Million"

โœ… Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

โฑ๏ธ Time and Space Complexity

  • Time Complexity: O(log n)
  • (Each digit is processed once.)
  • Space Complexity: O(1) auxiliary (ignoring output string)

โœ… Edge Cases

  • 0 โ†’ "Zero"
  • 1000000 โ†’ "One Million"
  • Leading zeros are ignored
  • Extra spaces are cleaned using .strip()

๐Ÿ“Œ Notes

  • You can add Billion safely as input is guaranteed to be โ‰ค 2ยณยน - 1
  • This is a great example of modular problem solving:
  • Break number into chunks
  • Handle small parts
  • Compose the answer

โœ… Conclusion

LeetCode 273: Integer to English Words is a practical, real-world problem teaching:

  • String decomposition
  • Recursion and modular logic
  • Human-readable formatting

Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex