< Summary

Information
Class: LeetCode.Algorithms.CheckIfParenthesesStringCanBeValid.CheckIfParenthesesStringCanBeValidGreedy
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CheckIfParenthesesStringCanBeValid\CheckIfParenthesesStringCanBeValidGreedy.cs
Line coverage
92%
Covered lines: 26
Uncovered lines: 2
Coverable lines: 28
Total lines: 60
Line coverage: 92.8%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanBeValid(...)90%101092.85%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CheckIfParenthesesStringCanBeValid\CheckIfParenthesesStringCanBeValidGreedy.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.CheckIfParenthesesStringCanBeValid;
 13
 14/// <inheritdoc />
 15public class CheckIfParenthesesStringCanBeValidGreedy : ICheckIfParenthesesStringCanBeValid
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <param name="locked"></param>
 23    /// <returns></returns>
 24    public bool CanBeValid(string s, string locked)
 425    {
 426        if (s.Length % 2 == 1)
 127        {
 128            return false;
 29        }
 30
 331        var openMin = 0;
 332        var openMax = 0;
 33
 9034        for (var i = 0; i < s.Length; i++)
 4235        {
 4236            if (locked[i] == '0')
 2537            {
 2538                openMax++;
 2539                openMin = Math.Max(0, openMin - 1);
 2540            }
 1741            else if (s[i] == '(')
 742            {
 743                openMax++;
 744                openMin++;
 745            }
 46            else
 1047            {
 1048                openMax--;
 1049                openMin = Math.Max(0, openMin - 1);
 1050            }
 51
 4252            if (openMax < 0)
 053            {
 054                return false;
 55            }
 4256        }
 57
 358        return openMin == 0;
 459    }
 60}