| | 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 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.IntegerToEnglishWords; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class IntegerToEnglishWordsIterative : IIntegerToEnglishWords |
| | 18 | | { |
| | 19 | | private const string Space = " "; |
| | 20 | |
|
| 27 | 21 | | private readonly Dictionary<int, string> _wordsDictionary = new() |
| 27 | 22 | | { |
| 27 | 23 | | { 1000000000, "Billion" }, |
| 27 | 24 | | { 1000000, "Million" }, |
| 27 | 25 | | { 1000, "Thousand" }, |
| 27 | 26 | | { 100, "Hundred" }, |
| 27 | 27 | | { 90, "Ninety" }, |
| 27 | 28 | | { 80, "Eighty" }, |
| 27 | 29 | | { 70, "Seventy" }, |
| 27 | 30 | | { 60, "Sixty" }, |
| 27 | 31 | | { 50, "Fifty" }, |
| 27 | 32 | | { 40, "Forty" }, |
| 27 | 33 | | { 30, "Thirty" }, |
| 27 | 34 | | { 20, "Twenty" }, |
| 27 | 35 | | { 19, "Nineteen" }, |
| 27 | 36 | | { 18, "Eighteen" }, |
| 27 | 37 | | { 17, "Seventeen" }, |
| 27 | 38 | | { 16, "Sixteen" }, |
| 27 | 39 | | { 15, "Fifteen" }, |
| 27 | 40 | | { 14, "Fourteen" }, |
| 27 | 41 | | { 13, "Thirteen" }, |
| 27 | 42 | | { 12, "Twelve" }, |
| 27 | 43 | | { 11, "Eleven" }, |
| 27 | 44 | | { 10, "Ten" }, |
| 27 | 45 | | { 9, "Nine" }, |
| 27 | 46 | | { 8, "Eight" }, |
| 27 | 47 | | { 7, "Seven" }, |
| 27 | 48 | | { 6, "Six" }, |
| 27 | 49 | | { 5, "Five" }, |
| 27 | 50 | | { 4, "Four" }, |
| 27 | 51 | | { 3, "Three" }, |
| 27 | 52 | | { 2, "Two" }, |
| 27 | 53 | | { 1, "One" }, |
| 27 | 54 | | { 0, "Zero" } |
| 27 | 55 | | }; |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Time complexity - O(log 10 num) |
| | 59 | | /// Space complexity - O(log 10 num) |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="num"></param> |
| | 62 | | /// <returns></returns> |
| | 63 | | public string NumberToWords(int num) |
| 27 | 64 | | { |
| 27 | 65 | | if (num == 0) |
| 1 | 66 | | { |
| 1 | 67 | | return _wordsDictionary[0]; |
| | 68 | | } |
| | 69 | |
|
| 26 | 70 | | var resultStringBuilder = new StringBuilder(); |
| | 71 | |
|
| 82 | 72 | | while (num > 0) |
| 56 | 73 | | { |
| 900 | 74 | | foreach (var word in _wordsDictionary.Where(word => num - word.Key >= 0)) |
| 56 | 75 | | { |
| 56 | 76 | | if (word.Key >= 100) |
| 29 | 77 | | { |
| 29 | 78 | | var count = num / word.Key; |
| | 79 | |
|
| 29 | 80 | | num -= count * word.Key; |
| | 81 | |
|
| 29 | 82 | | if (count >= 100) |
| 5 | 83 | | { |
| 5 | 84 | | var hundredsCount = count / 100; |
| | 85 | |
|
| 5 | 86 | | count -= hundredsCount * 100; |
| | 87 | |
|
| 5 | 88 | | resultStringBuilder.Append(_wordsDictionary[hundredsCount]); |
| 5 | 89 | | resultStringBuilder.Append(Space); |
| 5 | 90 | | resultStringBuilder.Append(_wordsDictionary[100]); |
| 5 | 91 | | resultStringBuilder.Append(Space); |
| 5 | 92 | | } |
| | 93 | |
|
| 29 | 94 | | if (count >= 20) |
| 6 | 95 | | { |
| 6 | 96 | | var tensCount = count / 10; |
| | 97 | |
|
| 6 | 98 | | count -= tensCount * 10; |
| | 99 | |
|
| 6 | 100 | | resultStringBuilder.Append(_wordsDictionary[tensCount * 10]); |
| 6 | 101 | | resultStringBuilder.Append(Space); |
| 6 | 102 | | } |
| | 103 | |
|
| 29 | 104 | | if (count >= 10) |
| 1 | 105 | | { |
| 1 | 106 | | resultStringBuilder.Append(_wordsDictionary[count]); |
| 1 | 107 | | resultStringBuilder.Append(Space); |
| | 108 | |
|
| 1 | 109 | | count = 0; |
| 1 | 110 | | } |
| | 111 | |
|
| 29 | 112 | | if (count >= 1) |
| 26 | 113 | | { |
| 26 | 114 | | resultStringBuilder.Append(_wordsDictionary[count]); |
| 26 | 115 | | resultStringBuilder.Append(Space); |
| 26 | 116 | | } |
| 29 | 117 | | } |
| | 118 | | else |
| 27 | 119 | | { |
| 27 | 120 | | num -= word.Key; |
| 27 | 121 | | } |
| | 122 | |
|
| 56 | 123 | | resultStringBuilder.Append(word.Value); |
| 56 | 124 | | resultStringBuilder.Append(Space); |
| | 125 | |
|
| 56 | 126 | | break; |
| | 127 | | } |
| 56 | 128 | | } |
| | 129 | |
|
| 26 | 130 | | return resultStringBuilder.ToString().TrimEnd(); |
| 27 | 131 | | } |
| | 132 | | } |