273. Integer to English Words - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Description:
Given a non-negative integer, convert it to its English words representation.

Example 1:

  • Input: 123
  • Output: "One Hundred Twenty Three"
  • Explanation:
    • The number 123 is broken down as:
      • Hundreds: One Hundred
      • Tens and Ones: Twenty Three

Example 2:

  • Input: 12345
  • Output: "Twelve Thousand Three Hundred Forty Five"
  • Explanation:
    • The number 12345 is segmented into two parts:
      • Thousand part: Twelve Thousand (from 12)
      • Remaining part: Three Hundred Forty Five (from 345)

Example 3:

  • Input: 1234567
  • Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
  • Explanation:
    • The number 1234567 is segmented as:
      • Million part: One Million (from 1)
      • Thousand part: Two Hundred Thirty Four Thousand (from 234)
      • Remaining part: Five Hundred Sixty Seven (from 567)

Constraints:

  • 0 <= num <= 2^31 - 1

2. Hints for the Solution

  • Hint 1:
    Break the number into chunks of three digits. Each chunk represents units, thousands, millions, or billions. This segmentation helps in reusing a helper function to convert numbers less than 1000.

  • Hint 2:
    Create helper mappings (arrays or dictionaries) for numbers less than 20, tens (20, 30, ... 90), and a mapping for the scales (Thousand, Million, Billion). Use these mappings to build the English representation for each three-digit segment.

Approaches to Solve the Problem

Approach 1: Naïve (Brute Force) Construction

Concept:

A brute force approach would try to process the number digit by digit (or group by group) by directly mapping each digit’s position to its English word. For instance, you could:

  • Convert the integer to a string.
  • Iterate through each character while keeping track of its positional value (ones, tens, hundreds, etc.).
  • Use numerous conditional checks (especially for special cases like numbers below 20 or tens such as “Twenty”, “Thirty”, etc.).

Issues with the Brute Force Approach:

  • Complexity in Handling Special Cases: Handling numbers from 10 to 19 (the “teens”) and numbers with zeros in between often leads to many if-else conditions.
  • Repetitive Code: This method may lead to repetitive logic and can be hard to maintain or extend.
  • Readability: The resulting code will be long and less intuitive, making it harder for others (or even yourself later) to understand.

Summary:

While the brute force approach is conceptually straightforward (mapping every digit with its place), it quickly becomes messy. This is why we recommend a more structured, divide-and-conquer approach.

Approach 2: Optimal Divide-and-Conquer (Recursive/Iterative Chunking)

Concept:

Break the integer into groups of three digits (since 1000 is the basic building block) and then convert each group using a helper function. After converting a group, append the corresponding scale (e.g., Thousand, Million, Billion).

Step-by-Step Walkthrough (Visual Example for 1,234,567):

  1. Segmentation:

    • Segment 1 (Rightmost 3 digits): 567 → Converts to "Five Hundred Sixty Seven"
    • Segment 2 (Next 3 digits): 234 → Converts to "Two Hundred Thirty Four"
    • Segment 3 (Remaining digit): 1 → Converts to "One"
  2. Scale Association:

    • For segment 1 (ones): no suffix
    • For segment 2: append "Thousand"
    • For segment 3: append "Million"
  3. Combine Segments:

    • Final answer: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Why This Approach is Optimal:

  • Reusability: The helper function for three-digit conversion is used repeatedly.
  • Modularity: Separates the concerns (converting small numbers vs. appending scale words).
  • Clarity: Each part of the number is handled in a consistent manner, leading to cleaner and more maintainable code.

Code Implementations

Python Implementation

Python3
Python3

. . . .

Java Implementation

Java
Java

. . . .

Complexity Analysis

  • Time Complexity:
    Even though the algorithm processes the number in chunks, the number of chunks is fixed (since the maximum number is 2^31 - 1, which only has up to 10 digits). In a theoretical scenario with an arbitrarily large number of digits (n digits), the approach would be O(n). For our constraints, it effectively runs in constant time.

  • Space Complexity:
    The space used for the recursion and for storing the output string is also proportional to the number of digits, which is again constant for 32-bit integers. Therefore, the space complexity is O(1).

Step-by-Step Walkthrough (Visual Example)

Let’s walk through converting 1,234,567:

  1. Break into chunks:

    • Chunk 1 (Least Significant 3 digits): 567
    • Chunk 2 (Next 3 digits): 234
    • Chunk 3 (Remaining): 1
  2. Convert each chunk using helper:

    • Chunk 1:
      • 567"Five Hundred " + helper(67)
      • 67tens[6] which is "Sixty " + helper(7)
      • 7"Seven "
      • Combined → "Five Hundred Sixty Seven "
    • Chunk 2:
      • 234"Two Hundred " + helper(34)
      • 34tens[3] which is "Thirty " + helper(4)
      • 4"Four "
      • Combined → "Two Hundred Thirty Four "
    • Chunk 3:
      • 1"One "
  3. Append scale words:

    • Chunk 1 (ones): No scale
    • Chunk 2: Append "Thousand"
    • Chunk 3: Append "Million"
  4. Final Combination:

    • "One Million " + "Two Hundred Thirty Four Thousand " + "Five Hundred Sixty Seven"
    • Result: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Common Mistakes & Edge Cases

Common Mistakes:

  • Forgetting to Handle Zero:
    The input 0 must return "Zero".
  • Improper Spacing:
    Extra spaces can creep in if you don’t trim the final string.
  • Handling Numbers Below 20:
    Not all numbers follow the standard tens-hundreds rule; numbers below 20 require special handling.
  • Neglecting Scale Suffixes:
    Ensure that every three-digit group is associated with the correct scale (Thousand, Million, etc.) when needed.

Edge Cases:

  • Input is Zero:
    Should immediately return "Zero".
  • Groups with Zeros:
    For numbers like 1000010, the middle group might be 000 which should be skipped.
  • Maximum Value:
    Ensure your solution works for 2^31 - 1 (i.e., 2,147,483,647).

Alternative Variations:

  • Integer to Roman Numerals:
    Converting integers to Roman numeral representations.
  • String to Integer (atoi):
    Parsing strings into integers while handling edge cases.
  • Integer to Words in Other Languages:
    Similar logic can be extended to languages other than English.

Related Problems for Further Practice:

  • Add Two Numbers:
    Practice handling number manipulations with linked lists.
  • Longest Palindromic Substring:
    Improve string manipulation skills.
  • Reverse Words in a String:
    Focus on string splitting and concatenation.
  • Binary Tree Level Order Traversal:
    Get comfortable with tree data structures and breadth-first search.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to write clean code in interviews?
What syntax does React use?
Incorporating mock deployments to explain system rollout plans
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;