| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.IntegerToEnglishWords; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class IntegerToEnglishWordsRecursive : IIntegerToEnglishWords |
| | 16 | | { |
| | 17 | | private const string Space = " "; |
| | 18 | | private const string Hundred = "Hundred"; |
| | 19 | |
|
| 1 | 20 | | private static readonly string[] BelowTwenty = |
| 1 | 21 | | [ |
| 1 | 22 | | "Zero", |
| 1 | 23 | | "One", |
| 1 | 24 | | "Two", |
| 1 | 25 | | "Three", |
| 1 | 26 | | "Four", |
| 1 | 27 | | "Five", |
| 1 | 28 | | "Six", |
| 1 | 29 | | "Seven", |
| 1 | 30 | | "Eight", |
| 1 | 31 | | "Nine", |
| 1 | 32 | | "Ten", |
| 1 | 33 | | "Eleven", |
| 1 | 34 | | "Twelve", |
| 1 | 35 | | "Thirteen", |
| 1 | 36 | | "Fourteen", |
| 1 | 37 | | "Fifteen", |
| 1 | 38 | | "Sixteen", |
| 1 | 39 | | "Seventeen", |
| 1 | 40 | | "Eighteen", |
| 1 | 41 | | "Nineteen" |
| 1 | 42 | | ]; |
| | 43 | |
|
| 1 | 44 | | private static readonly string[] Tens = |
| 1 | 45 | | [ |
| 1 | 46 | | "", |
| 1 | 47 | | "", |
| 1 | 48 | | "Twenty", |
| 1 | 49 | | "Thirty", |
| 1 | 50 | | "Forty", |
| 1 | 51 | | "Fifty", |
| 1 | 52 | | "Sixty", |
| 1 | 53 | | "Seventy", |
| 1 | 54 | | "Eighty", |
| 1 | 55 | | "Ninety" |
| 1 | 56 | | ]; |
| | 57 | |
|
| 1 | 58 | | private static readonly string[] Thousands = ["", "Thousand", "Million", "Billion"]; |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Time complexity - O(log 10 num) |
| | 62 | | /// Space complexity - O(log 10 num) |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="num"></param> |
| | 65 | | /// <returns></returns> |
| | 66 | | public string NumberToWords(int num) |
| 27 | 67 | | { |
| 27 | 68 | | if (num == 0) |
| 1 | 69 | | { |
| 1 | 70 | | return BelowTwenty[0]; |
| | 71 | | } |
| | 72 | |
|
| 26 | 73 | | var i = 0; |
| 26 | 74 | | var words = string.Empty; |
| | 75 | |
|
| 71 | 76 | | while (num > 0) |
| 45 | 77 | | { |
| 45 | 78 | | if (num % 1000 != 0) |
| 37 | 79 | | { |
| 37 | 80 | | words = Helper(num % 1000) + Thousands[i] + Space + words; |
| 37 | 81 | | } |
| | 82 | |
|
| 45 | 83 | | num /= 1000; |
| | 84 | |
|
| 45 | 85 | | i++; |
| 45 | 86 | | } |
| | 87 | |
|
| 26 | 88 | | return words.Trim(); |
| 27 | 89 | | } |
| | 90 | |
|
| | 91 | | private static string Helper(int num) |
| 69 | 92 | | { |
| 69 | 93 | | return num switch |
| 69 | 94 | | { |
| 4 | 95 | | 0 => string.Empty, |
| 33 | 96 | | < 20 => BelowTwenty[num] + Space, |
| 14 | 97 | | < 100 => Tens[num / 10] + Space + Helper(num % 10), |
| 18 | 98 | | _ => BelowTwenty[num / 100] + Space + Hundred + Space + Helper(num % 100) |
| 69 | 99 | | }; |
| 69 | 100 | | } |
| | 101 | | } |