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