273. Integer to English Words - Detailed Explanation
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
- Hundreds:
- The number 123 is broken down as:
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)
- Thousand part:
- The number 12345 is segmented into two parts:
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)
- Million part:
- The number 1234567 is segmented as:
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):
-
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"
- Segment 1 (Rightmost 3 digits):
-
Scale Association:
- For segment 1 (ones): no suffix
- For segment 2: append
"Thousand"
- For segment 3: append
"Million"
-
Combine Segments:
- Final answer:
"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
- Final answer:
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
Java Implementation
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
:
-
Break into chunks:
- Chunk 1 (Least Significant 3 digits):
567
- Chunk 2 (Next 3 digits):
234
- Chunk 3 (Remaining):
1
- Chunk 1 (Least Significant 3 digits):
-
Convert each chunk using
helper
:- Chunk 1:
567
→"Five Hundred "
+ helper(67)67
→tens[6]
which is"Sixty "
+ helper(7)7
→"Seven "
- Combined →
"Five Hundred Sixty Seven "
- Chunk 2:
234
→"Two Hundred "
+ helper(34)34
→tens[3]
which is"Thirty "
+ helper(4)4
→"Four "
- Combined →
"Two Hundred Thirty Four "
- Chunk 3:
1
→"One "
- Chunk 1:
-
Append scale words:
- Chunk 1 (ones): No scale
- Chunk 2: Append
"Thousand"
- Chunk 3: Append
"Million"
-
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 input0
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 like1000010
, the middle group might be000
which should be skipped. - Maximum Value:
Ensure your solution works for2^31 - 1
(i.e., 2,147,483,647).
Alternative Variations and Related Problems
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.
GET YOUR FREE
Coding Questions Catalog
