< Summary

Information
Class: LeetCode.Algorithms.ZigzagConversion.ZigzagConversionSimulation
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ZigzagConversion\ZigzagConversionSimulation.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Convert(...)100%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ZigzagConversion\ZigzagConversionSimulation.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.ZigzagConversion;
 15
 16/// <inheritdoc />
 17public class ZigzagConversionSimulation : IZigzagConversion
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="s"></param>
 24    /// <param name="numRows"></param>
 25    /// <returns></returns>
 26    public string Convert(string s, int numRows)
 327    {
 328        if (numRows == 1 || s.Length <= numRows)
 129        {
 130            return s;
 31        }
 32
 233        var stringBuilders = new StringBuilder[numRows];
 34
 1835        for (var i = 0; i < stringBuilders.Length; i++)
 736        {
 737            stringBuilders[i] = new StringBuilder();
 738        }
 39
 240        var index = 0;
 41
 242        var goingDown = false;
 43
 6244        foreach (var c in s)
 2845        {
 2846            stringBuilders[index].Append(c);
 47
 2848            if (index == 0 || index == numRows - 1)
 1249            {
 1250                goingDown = !goingDown;
 1251            }
 52
 2853            index += goingDown ? 1 : -1;
 2854        }
 55
 256        var resultStringBuilder = new StringBuilder();
 57
 2058        foreach (var stringBuilder in stringBuilders)
 759        {
 760            resultStringBuilder.Append(stringBuilder);
 761        }
 62
 263        return resultStringBuilder.ToString();
 364    }
 65}