< Summary

Information
Class: LeetCode.Algorithms.MinimumRemoveToMakeValidParentheses.MinimumRemoveToMakeValidParenthesesStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumRemoveToMakeValidParentheses\MinimumRemoveToMakeValidParenthesesStack.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 63
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinRemoveToMakeValid(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumRemoveToMakeValidParentheses\MinimumRemoveToMakeValidParenthesesStack.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
 12using System.Text;
 13
 14namespace LeetCode.Algorithms.MinimumRemoveToMakeValidParentheses;
 15
 16/// <inheritdoc />
 17public class MinimumRemoveToMakeValidParenthesesStack : IMinimumRemoveToMakeValidParentheses
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="s"></param>
 24    /// <returns></returns>
 25    public string MinRemoveToMakeValid(string s)
 326    {
 327        var removeIndices = new HashSet<int>();
 328        var stack = new Stack<int>();
 29
 5430        for (var i = 0; i < s.Length; i++)
 2431        {
 2432            switch (s[i])
 33            {
 34                case '(':
 535                    stack.Push(i);
 536                    break;
 737                case ')' when stack.Count > 0:
 338                    stack.Pop();
 339                    break;
 40                case ')':
 441                    removeIndices.Add(i);
 442                    break;
 43            }
 2444        }
 45
 546        while (stack.Count > 0)
 247        {
 248            removeIndices.Add(stack.Pop());
 249        }
 50
 351        var resultBuilder = new StringBuilder();
 52
 5453        for (var i = 0; i < s.Length; i++)
 2454        {
 2455            if (!removeIndices.Contains(i))
 1856            {
 1857                resultBuilder.Append(s[i]);
 1858            }
 2459        }
 60
 361        return resultBuilder.ToString();
 362    }
 63}