< Summary

Information
Class: LeetCode.Algorithms.MinimumStringLengthAfterRemovingSubstrings.MinimumStringLengthAfterRemovingSubstringsStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumStringLengthAfterRemovingSubstrings\MinimumStringLengthAfterRemovingSubstringsStack.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 54
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
MinLength(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumStringLengthAfterRemovingSubstrings\MinimumStringLengthAfterRemovingSubstringsStack.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.MinimumStringLengthAfterRemovingSubstrings;
 13
 14/// <inheritdoc />
 15public class MinimumStringLengthAfterRemovingSubstringsStack : IMinimumStringLengthAfterRemovingSubstrings
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <returns></returns>
 23    public int MinLength(string s)
 224    {
 225        var minLength = s.Length;
 26
 227        var stack = new Stack<char>();
 28
 3229        foreach (var c in s)
 1330        {
 1331            switch (c)
 32            {
 433                case 'B' when stack.Count > 0 && stack.Peek() == 'A':
 234                    stack.Pop();
 35
 236                    minLength -= 2;
 37
 238                    break;
 239                case 'D' when stack.Count > 0 && stack.Peek() == 'C':
 140                    stack.Pop();
 41
 142                    minLength -= 2;
 43
 144                    break;
 45                default:
 1046                    stack.Push(c);
 47
 1048                    break;
 49            }
 1350        }
 51
 252        return minLength;
 253    }
 54}

Methods/Properties

MinLength(System.String)