< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
NumberToWords(...)100%66100%
Helper(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IntegerToEnglishWords\IntegerToEnglishWordsRecursive.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
 12namespace LeetCode.Algorithms.IntegerToEnglishWords;
 13
 14/// <inheritdoc />
 15public class IntegerToEnglishWordsRecursive : IIntegerToEnglishWords
 16{
 17    private const string Space = " ";
 18    private const string Hundred = "Hundred";
 19
 120    private static readonly string[] BelowTwenty =
 121    [
 122        "Zero",
 123        "One",
 124        "Two",
 125        "Three",
 126        "Four",
 127        "Five",
 128        "Six",
 129        "Seven",
 130        "Eight",
 131        "Nine",
 132        "Ten",
 133        "Eleven",
 134        "Twelve",
 135        "Thirteen",
 136        "Fourteen",
 137        "Fifteen",
 138        "Sixteen",
 139        "Seventeen",
 140        "Eighteen",
 141        "Nineteen"
 142    ];
 43
 144    private static readonly string[] Tens =
 145    [
 146        "",
 147        "",
 148        "Twenty",
 149        "Thirty",
 150        "Forty",
 151        "Fifty",
 152        "Sixty",
 153        "Seventy",
 154        "Eighty",
 155        "Ninety"
 156    ];
 57
 158    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)
 2767    {
 2768        if (num == 0)
 169        {
 170            return BelowTwenty[0];
 71        }
 72
 2673        var i = 0;
 2674        var words = string.Empty;
 75
 7176        while (num > 0)
 4577        {
 4578            if (num % 1000 != 0)
 3779            {
 3780                words = Helper(num % 1000) + Thousands[i] + Space + words;
 3781            }
 82
 4583            num /= 1000;
 84
 4585            i++;
 4586        }
 87
 2688        return words.Trim();
 2789    }
 90
 91    private static string Helper(int num)
 6992    {
 6993        return num switch
 6994        {
 495            0 => string.Empty,
 3396            < 20 => BelowTwenty[num] + Space,
 1497            < 100 => Tens[num / 10] + Space + Helper(num % 10),
 1898            _ => BelowTwenty[num / 100] + Space + Hundred + Space + Helper(num % 100)
 6999        };
 69100    }
 101}