Integer to English Words
LeetCode 273 | Difficulty: Hardβ
HardProblem Descriptionβ
Convert a non-negative integer num to its English words representation.
Example 1:
Input: num = 123
Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Constraints:
- `0 <= num <= 2^31 - 1`
Topics: Math, String, Recursion
Approachβ
Mathematicalβ
Look for mathematical patterns or formulas. Consider: modular arithmetic, GCD/LCM, prime factorization, combinatorics, or geometric properties.
Problems with clear mathematical structure, counting, number properties.
String Processingβ
Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.
Anagram detection, palindrome checking, string transformation, pattern matching.
Solutionsβ
Solution 1: C# (Best: 72 ms)β
| Metric | Value |
|---|---|
| Runtime | 72 ms |
| Memory | 36.9 MB |
| Date | 2021-11-30 |
public class Solution {
private string[] belowTen = new string[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
private string[] belowTwenty = new string[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private string[] tens = new string[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
public string NumberToWords(int num)
{
if(num == 0) return "Zero";
return helper(num);
}
private string helper(int num)
{
string result = string.Empty;
if(num<10)
{
result = belowTen[num];
}
else if(num<20)
{
result = belowTwenty[num%10];
}
else if(num <100)
{
result = tens[num/10] + " " + belowTen[num%10];
}
else if(num<1000)
{
result = helper(num/100) + " Hundred "+ helper(num%100);
}
else if(num<1000000)
{
result = helper(num/1000) + " Thousand "+ helper(num%1000);
}
else if (num < 1000000000)
{
result = helper(num / 1000000) + " Million " + helper(num % 1000000);
}
else result = helper(num / 1000000000) + " Billion " + helper(num % 1000000000);
return result.Trim();
}
}
π 1 more C# submission(s)
Submission (2017-11-07) β 122 ms, N/Aβ
public class Solution {
private static readonly string[] belowTen = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
private static readonly string[] belowTwenty = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
private static readonly string[] belowHundred = new string[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public string NumberToWords(int num)
{
if(num==0) return "Zero";
return Helper(num);
}
private string Helper(int num)
{
string result = string.Empty;
if(num<10) result = belowTen[num];
else if (num < 20) result = belowTwenty[num%10];
else if (num < 100) result = belowHundred[num / 10] + " " + belowTen[num % 10] + result;
else if (num < 1000) result = Helper(num / 100) + " Hundred " + Helper(num % 100);
else if (num < 1000000) result = Helper(num / 1000) + " Thousand " + Helper(num % 1000);
else if (num < 1000000000) result = Helper(num / 1000000) + " Million " + Helper(num % 1000000);
else result = Helper(num / 1000000000) + " Billion " + Helper(num % 1000000000);
return result.Trim();
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | $O(n)$ | $O(1) to O(n)$ |
Interview Tipsβ
- Break the problem into smaller subproblems. Communicate your approach before coding.
- LeetCode provides 3 hint(s) for this problem β try solving without them first.
π‘ Hints
Hint 1: Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
Hint 2: Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
Hint 3: There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)