< Summary

Information
Class: LeetCode.Algorithms.DesignSpreadsheet.DesignSpreadsheetMatrix
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignSpreadsheet\DesignSpreadsheetMatrix.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 129
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%
SetCell(...)100%11100%
ResetCell(...)100%11100%
GetValue(...)100%11100%
ParseToken(...)100%22100%
ParseCell(...)100%11100%
ParseValue(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignSpreadsheet\DesignSpreadsheetMatrix.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.DesignSpreadsheet;
 13
 14/// <inheritdoc />
 15public sealed class DesignSpreadsheetMatrix : IDesignSpreadsheet
 16{
 17    private const int Columns = 'Z' - 'A' + 1;
 18
 19    private readonly int[,] _spreadsheet;
 20
 21    /// <summary>
 22    ///     Time complexity - O(n), where n is rows
 23    ///     Space complexity - O(n), where n is rows
 24    /// </summary>
 25    /// <param name="rows"></param>
 126    public DesignSpreadsheetMatrix(int rows)
 127    {
 128        _spreadsheet = new int[rows, Columns];
 129    }
 30
 31    /// <summary>
 32    ///     Time complexity - O(m), where m is the cell length
 33    ///     Space complexity - O(1)
 34    /// </summary>
 35    /// <param name="cell"></param>
 36    /// <param name="value"></param>
 37    public void SetCell(string cell, int value)
 338    {
 339        var (row, column) = ParseCell(cell, 0, cell.Length);
 40
 341        _spreadsheet[row, column] = value;
 342    }
 43
 44    /// <summary>
 45    ///     Time complexity - O(m), where m is the cell length
 46    ///     Space complexity - O(1)
 47    /// </summary>
 48    /// <param name="cell"></param>
 49    public void ResetCell(string cell)
 150    {
 151        SetCell(cell, 0);
 152    }
 53
 54    /// <summary>
 55    ///     Time complexity - O(m), where m is the formula length
 56    ///     Space complexity - O(1)
 57    /// </summary>
 58    /// <param name="formula"></param>
 59    /// <returns></returns>
 60    public int GetValue(string formula)
 461    {
 462        var indexOfPlus = formula.IndexOf('+');
 63
 464        var left = ParseToken(formula, 1, indexOfPlus);
 65
 466        var right = ParseToken(formula, indexOfPlus + 1, formula.Length);
 67
 468        return left + right;
 469    }
 70
 71    /// <summary>
 72    ///     Time complexity - O(m), where m is the s length
 73    ///     Space complexity - O(1)
 74    /// </summary>
 75    /// <param name="s"></param>
 76    /// <param name="start"></param>
 77    /// <param name="end"></param>
 78    /// <returns></returns>
 79    private int ParseToken(string s, int start, int end)
 880    {
 881        if (char.IsDigit(s[start]))
 382        {
 383            return ParseValue(s, start, end);
 84        }
 85
 586        var (row, column) = ParseCell(s, start, end);
 87
 588        return _spreadsheet[row, column];
 889    }
 90
 91    /// <summary>
 92    ///     Time complexity - O(m), where m is the length of the s string
 93    ///     Space complexity - O(1)
 94    /// </summary>
 95    /// <param name="s"></param>
 96    /// <param name="start"></param>
 97    /// <param name="end"></param>
 98    /// <returns></returns>
 99    private static (int Row, int Column) ParseCell(string s, int start, int end)
 8100    {
 8101        var column = s[start] - 'A';
 102
 8103        var row = ParseValue(s, start + 1, end) - 1;
 104
 8105        return (row, column);
 8106    }
 107
 108    /// <summary>
 109    ///     Time complexity - O(m), where m is the s length
 110    ///     Space complexity - O(1)
 111    /// </summary>
 112    /// <param name="s"></param>
 113    /// <param name="start"></param>
 114    /// <param name="end"></param>
 115    /// <returns></returns>
 116    private static int ParseValue(string s, int start, int end)
 11117    {
 11118        var value = 0;
 119
 44120        for (var i = start; i < end; i++)
 11121        {
 11122            var c = s[i];
 123
 11124            value = (value * 10) + (c - '0');
 11125        }
 126
 11127        return value;
 11128    }
 129}