Integer to English Words
Convert a non-negative integer into its English word representation. This is a common task in natural language processing and user interface design, where large numbers need to be communicated clearly and understandably.
Problem Description
The goal is to write a function that takes a non-negative integer as input and returns its equivalent English word representation. For example, 123 should be "One Hundred Twenty Three", and 12345 should be "Twelve Thousand Three Hundred Forty Five".
Key Requirements:
- Handle numbers from 0 up to a specified maximum limit.
- Produce grammatically correct English words.
- Follow standard English number naming conventions (e.g., "hundred", "thousand", "million", "billion").
- Do not use "and" when constructing numbers (e.g., "One Hundred Twenty Three", not "One Hundred and Twenty Three").
- Numbers should be in title case (first letter capitalized, the rest lowercase, except for proper nouns if any, though none are expected here).
Edge Cases to Consider:
- Zero (0)
- Numbers less than 20 (e.g., eleven, twelve, thirteen)
- Numbers between 20 and 99 (e.g., twenty, thirty, forty)
- Numbers that are exact multiples of 100, 1000, 1,000,000, etc.
- Large numbers requiring multiple magnitude words.
Examples
Example 1:
Input: 123
Output: One Hundred Twenty Three
Explanation: The number 123 is broken down into its hundreds (100) and tens/ones (23). 100 is "One Hundred", and 23 is "Twenty Three".
Example 2:
Input: 12345
Output: Twelve Thousand Three Hundred Forty Five
Explanation: The number 12345 is broken into thousands (12,000) and the remainder (345). 12,000 is "Twelve Thousand", and 345 is "Three Hundred Forty Five".
Example 3:
Input: 1000000
Output: One Million
Explanation: This is an exact multiple of a large magnitude.
Example 4:
Input: 0
Output: Zero
Explanation: The special case for zero.
Constraints
- The input integer will be non-negative.
- The input integer will be less than 2<sup>31</sup> - 1 (standard 32-bit signed integer maximum).
- The output should be a string.
- The conversion process should be efficient, ideally with a time complexity related to the number of digits in the input number rather than its magnitude.
Notes
Consider breaking down the problem into smaller, manageable parts. You might find it helpful to:
- Handle the conversion of numbers from 0 to 19.
- Handle the conversion of multiples of 10 (twenty, thirty, etc.).
- Handle the conversion of numbers from 0 to 99.
- Handle numbers from 0 to 999, utilizing the "hundred" word.
- Finally, extend this to larger magnitudes like thousands, millions, and billions.
Think about how to structure your logic to avoid repetitive code. A lookup table or a series of helper functions could be beneficial.