< Summary

Information
Class: LeetCode.Algorithms.ExcelSheetColumnNumber.ExcelSheetColumnNumberIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ExcelSheetColumnNumber\ExcelSheetColumnNumberIterative.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
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%
TitleToNumber(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ExcelSheetColumnNumber\ExcelSheetColumnNumberIterative.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.ExcelSheetColumnNumber;
 13
 14/// <inheritdoc />
 15public class ExcelSheetColumnNumberIterative : IExcelSheetColumnNumber
 16{
 17    private const int ColumnsCount = 26;
 18
 1219    private readonly Dictionary<char, int> _columnsDictionary = new()
 1220    {
 1221        { 'A', 1 },
 1222        { 'B', 2 },
 1223        { 'C', 3 },
 1224        { 'D', 4 },
 1225        { 'E', 5 },
 1226        { 'F', 6 },
 1227        { 'G', 7 },
 1228        { 'H', 8 },
 1229        { 'I', 9 },
 1230        { 'J', 10 },
 1231        { 'K', 11 },
 1232        { 'L', 12 },
 1233        { 'M', 13 },
 1234        { 'N', 14 },
 1235        { 'O', 15 },
 1236        { 'P', 16 },
 1237        { 'Q', 17 },
 1238        { 'R', 18 },
 1239        { 'S', 19 },
 1240        { 'T', 20 },
 1241        { 'U', 21 },
 1242        { 'V', 22 },
 1243        { 'W', 23 },
 1244        { 'X', 24 },
 1245        { 'Y', 25 },
 1246        { 'Z', 26 }
 1247    };
 48
 49    /// <summary>
 50    ///     Time complexity - O(n)
 51    ///     Space complexity - O(1)
 52    /// </summary>
 53    public int TitleToNumber(string columnTitle)
 1254    {
 1255        var number = 0;
 56
 9257        for (var i = columnTitle.Length - 1; i >= 0; i--)
 3458        {
 3459            number += _columnsDictionary[columnTitle[i]] * (int)Math.Pow(ColumnsCount, columnTitle.Length - 1 - i);
 3460        }
 61
 1262        return number;
 1263    }
 64}