< Summary

Information
Class: LeetCode.Algorithms.IntegerToEnglishWords.IntegerToEnglishWordsIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IntegerToEnglishWords\IntegerToEnglishWordsIterative.cs
Line coverage
100%
Covered lines: 85
Uncovered lines: 0
Coverable lines: 85
Total lines: 132
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
NumberToWords(...)100%1818100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IntegerToEnglishWords\IntegerToEnglishWordsIterative.cs

#LineLine coverage
 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
 12using System.Text;
 13
 14namespace LeetCode.Algorithms.IntegerToEnglishWords;
 15
 16/// <inheritdoc />
 17public class IntegerToEnglishWordsIterative : IIntegerToEnglishWords
 18{
 19    private const string Space = " ";
 20
 2721    private readonly Dictionary<int, string> _wordsDictionary = new()
 2722    {
 2723        { 1000000000, "Billion" },
 2724        { 1000000, "Million" },
 2725        { 1000, "Thousand" },
 2726        { 100, "Hundred" },
 2727        { 90, "Ninety" },
 2728        { 80, "Eighty" },
 2729        { 70, "Seventy" },
 2730        { 60, "Sixty" },
 2731        { 50, "Fifty" },
 2732        { 40, "Forty" },
 2733        { 30, "Thirty" },
 2734        { 20, "Twenty" },
 2735        { 19, "Nineteen" },
 2736        { 18, "Eighteen" },
 2737        { 17, "Seventeen" },
 2738        { 16, "Sixteen" },
 2739        { 15, "Fifteen" },
 2740        { 14, "Fourteen" },
 2741        { 13, "Thirteen" },
 2742        { 12, "Twelve" },
 2743        { 11, "Eleven" },
 2744        { 10, "Ten" },
 2745        { 9, "Nine" },
 2746        { 8, "Eight" },
 2747        { 7, "Seven" },
 2748        { 6, "Six" },
 2749        { 5, "Five" },
 2750        { 4, "Four" },
 2751        { 3, "Three" },
 2752        { 2, "Two" },
 2753        { 1, "One" },
 2754        { 0, "Zero" }
 2755    };
 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)
 2764    {
 2765        if (num == 0)
 166        {
 167            return _wordsDictionary[0];
 68        }
 69
 2670        var resultStringBuilder = new StringBuilder();
 71
 8272        while (num > 0)
 5673        {
 90074            foreach (var word in _wordsDictionary.Where(word => num - word.Key >= 0))
 5675            {
 5676                if (word.Key >= 100)
 2977                {
 2978                    var count = num / word.Key;
 79
 2980                    num -= count * word.Key;
 81
 2982                    if (count >= 100)
 583                    {
 584                        var hundredsCount = count / 100;
 85
 586                        count -= hundredsCount * 100;
 87
 588                        resultStringBuilder.Append(_wordsDictionary[hundredsCount]);
 589                        resultStringBuilder.Append(Space);
 590                        resultStringBuilder.Append(_wordsDictionary[100]);
 591                        resultStringBuilder.Append(Space);
 592                    }
 93
 2994                    if (count >= 20)
 695                    {
 696                        var tensCount = count / 10;
 97
 698                        count -= tensCount * 10;
 99
 6100                        resultStringBuilder.Append(_wordsDictionary[tensCount * 10]);
 6101                        resultStringBuilder.Append(Space);
 6102                    }
 103
 29104                    if (count >= 10)
 1105                    {
 1106                        resultStringBuilder.Append(_wordsDictionary[count]);
 1107                        resultStringBuilder.Append(Space);
 108
 1109                        count = 0;
 1110                    }
 111
 29112                    if (count >= 1)
 26113                    {
 26114                        resultStringBuilder.Append(_wordsDictionary[count]);
 26115                        resultStringBuilder.Append(Space);
 26116                    }
 29117                }
 118                else
 27119                {
 27120                    num -= word.Key;
 27121                }
 122
 56123                resultStringBuilder.Append(word.Value);
 56124                resultStringBuilder.Append(Space);
 125
 56126                break;
 127            }
 56128        }
 129
 26130        return resultStringBuilder.ToString().TrimEnd();
 27131    }
 132}