< Summary

Information
Class: LeetCode.Algorithms.CountAndSay.CountAndSayIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountAndSay\CountAndSayIterative.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 60
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountAndSay(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountAndSay\CountAndSayIterative.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.CountAndSay;
 15
 16/// <inheritdoc />
 17public class CountAndSayIterative : ICountAndSay
 18{
 19    /// <summary>
 20    ///     Time complexity - O(2^n)
 21    ///     Space complexity - O(2^n)
 22    /// </summary>
 23    /// <param name="n"></param>
 24    /// <returns></returns>
 25    public string CountAndSay(int n)
 426    {
 427        var result = "1";
 28
 2029        for (var i = 1; i < n; i++)
 630        {
 631            var stringBuilder = new StringBuilder();
 32
 633            var previous = result[0];
 634            var count = 1;
 35
 1836            for (var j = 1; j < result.Length; j++)
 337            {
 338                if (previous == result[j])
 239                {
 240                    count++;
 241                }
 42                else
 143                {
 144                    stringBuilder.Append(count);
 145                    stringBuilder.Append(previous);
 46
 147                    previous = result[j];
 148                    count = 1;
 149                }
 350            }
 51
 652            stringBuilder.Append(count);
 653            stringBuilder.Append(previous);
 54
 655            result = stringBuilder.ToString();
 656        }
 57
 458        return result;
 459    }
 60}

Methods/Properties

CountAndSay(System.Int32)