< Summary

Information
Class: LeetCode.Algorithms.RomanToInteger.RomanToIntegerDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RomanToInteger\RomanToIntegerDictionary.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
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%
RomanToInt(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RomanToInteger\RomanToIntegerDictionary.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.RomanToInteger;
 13
 14/// <inheritdoc />
 15public class RomanToIntegerDictionary : IRomanToInteger
 16{
 317    private readonly Dictionary<string, int> _romanIntegersDictionary = new()
 318    {
 319        { "IV", 4 },
 320        { "IX", 9 },
 321        { "XL", 40 },
 322        { "XC", 90 },
 323        { "CD", 400 },
 324        { "CM", 900 },
 325        { "I", 1 },
 326        { "V", 5 },
 327        { "X", 10 },
 328        { "L", 50 },
 329        { "C", 100 },
 330        { "D", 500 },
 331        { "M", 1000 }
 332    };
 33
 34    /// <summary>
 35    ///     Time complexity - O(n)
 36    ///     Space complexity - O(1)
 37    /// </summary>
 38    /// <param name="romanString"></param>
 39    /// <returns></returns>
 40    public int RomanToInt(string romanString)
 341    {
 342        var result = 0;
 343        var i = 0;
 44
 1545        while (i < romanString.Length)
 1246        {
 1247            if (i < romanString.Length - 1 &&
 1248                _romanIntegersDictionary.TryGetValue(romanString.Substring(i, 2), out var value))
 349            {
 350                result += value;
 351                i += 2;
 352            }
 53            else
 954            {
 955                if (_romanIntegersDictionary.TryGetValue(romanString.Substring(i, 1), out var value1))
 956                {
 957                    result += value1;
 958                }
 59
 960                i++;
 961            }
 1262        }
 63
 364        return result;
 365    }
 66}

Methods/Properties

.ctor()
RomanToInt(System.String)