< Summary

Information
Class: LeetCode.Algorithms.ParsingBooleanExpression.ParsingBooleanExpressionRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ParsingBooleanExpression\ParsingBooleanExpressionRecursive.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 79
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ParseBoolExpr(...)100%2222100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ParsingBooleanExpression\ParsingBooleanExpressionRecursive.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.ParsingBooleanExpression;
 13
 14/// <inheritdoc />
 15public class ParsingBooleanExpressionRecursive : IParsingBooleanExpression
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="expression"></param>
 22    /// <returns></returns>
 23    public bool ParseBoolExpr(string expression)
 1224    {
 1225        if (expression.Length == 1)
 726        {
 727            return expression == "t";
 28        }
 29
 530        var operation = expression[0];
 31
 532        if (operation == '!')
 133        {
 134            return !ParseBoolExpr(expression.Substring(2, expression.Length - 3));
 35        }
 36
 437        var result = operation == '&';
 38
 439        var level = 0;
 440        var start = 2;
 41
 4642        for (var i = start; i < expression.Length; i++)
 1943        {
 1944            var character = expression[i];
 45
 1946            switch (character)
 47            {
 48                case '(':
 149                    level++;
 150                    break;
 51                case ')':
 552                    level--;
 553                    break;
 54            }
 55
 1956            if ((character != ',' || level != 0) && (character != ')' || level != -1))
 1157            {
 1158                continue;
 59            }
 60
 861            var subExpression = expression.Substring(start, i - start);
 862            var subResult = ParseBoolExpr(subExpression);
 63
 864            switch (operation)
 65            {
 66                case '&':
 367                    result &= subResult;
 368                    break;
 69                case '|':
 570                    result |= subResult;
 571                    break;
 72            }
 73
 874            start = i + 1;
 875        }
 76
 477        return result;
 1278    }
 79}

Methods/Properties

ParseBoolExpr(System.String)