< Summary

Information
Class: LeetCode.Algorithms.FractionAdditionAndSubtraction.FractionAdditionAndSubtractionSimulation
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FractionAdditionAndSubtraction\FractionAdditionAndSubtractionSimulation.cs
Line coverage
100%
Covered lines: 49
Uncovered lines: 0
Coverable lines: 49
Total lines: 95
Line coverage: 100%
Branch coverage
94%
Covered branches: 17
Total branches: 18
Branch coverage: 94.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FractionAddition(...)93.75%1616100%
GetDivisor(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FractionAdditionAndSubtraction\FractionAdditionAndSubtractionSimulation.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.FractionAdditionAndSubtraction;
 13
 14/// <inheritdoc />
 15public class FractionAdditionAndSubtractionSimulation : IFractionAdditionAndSubtraction
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="expression"></param>
 22    /// <returns></returns>
 23    public string FractionAddition(string expression)
 1024    {
 1025        var numerator = 0;
 1026        var denominator = 1;
 1027        var i = 0;
 28
 6429        while (i < expression.Length)
 5430        {
 5431            var sign = 1;
 32
 5433            switch (expression[i])
 34            {
 35                case '-':
 3536                    sign = -1;
 37
 3538                    i++;
 3539                    break;
 40                case '+':
 1441                    i++;
 1442                    break;
 43            }
 44
 5445            var currentNumerator = 0;
 46
 10947            while (i < expression.Length && char.IsDigit(expression[i]))
 5548            {
 5549                currentNumerator = (currentNumerator * 10) + (int)char.GetNumericValue(expression[i]);
 50
 5551                i++;
 5552            }
 53
 5454            currentNumerator *= sign;
 55
 5456            i++;
 57
 5458            var currentDenominator = 0;
 59
 11660            while (i < expression.Length && char.IsDigit(expression[i]))
 6261            {
 6262                currentDenominator = (currentDenominator * 10) + (int)char.GetNumericValue(expression[i]);
 6263                i++;
 6264            }
 65
 5466            numerator = (numerator * currentDenominator) + (currentNumerator * denominator);
 5467            denominator *= currentDenominator;
 68
 5469            var divisor = GetDivisor(numerator, denominator);
 70
 5471            numerator /= divisor;
 5472            denominator /= divisor;
 5473        }
 74
 1075        if (denominator < 0)
 176        {
 177            numerator = -numerator;
 178            denominator = -denominator;
 179        }
 80
 1081        return $"{numerator}/{denominator}";
 1082    }
 83
 84    private static int GetDivisor(int numerator, int denominator)
 5485    {
 24586        while (denominator != 0)
 19187        {
 19188            var temp = denominator;
 19189            denominator = numerator % denominator;
 19190            numerator = temp;
 19191        }
 92
 5493        return numerator;
 5494    }
 95}