< Summary

Information
Class: LeetCode.Algorithms.RemoveAllOccurrencesOfSubstring.RemoveAllOccurrencesOfSubstringStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RemoveAllOccurrencesOfSubstring\RemoveAllOccurrencesOfSubstringStack.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RemoveOccurrences(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RemoveAllOccurrencesOfSubstring\RemoveAllOccurrencesOfSubstringStack.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.RemoveAllOccurrencesOfSubstring;
 13
 14/// <inheritdoc />
 15public class RemoveAllOccurrencesOfSubstringStack : IRemoveAllOccurrencesOfSubstring
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <param name="part"></param>
 23    /// <returns></returns>
 24    public string RemoveOccurrences(string s, string part)
 325    {
 326        var stack = new Stack<char>();
 27
 12928        foreach (var c in s)
 6029        {
 6030            stack.Push(c);
 31
 6032            if (stack.Count < part.Length)
 3233            {
 3234                continue;
 35            }
 36
 2837            var tempStack = new Stack<char>();
 38
 14439            for (var i = part.Length - 1; i >= 0; i--)
 6340            {
 6341                if (stack.Peek() == part[i])
 4442                {
 4443                    tempStack.Push(stack.Pop());
 4444                }
 45                else
 1946                {
 2047                    while (tempStack.Count > 0)
 148                    {
 149                        stack.Push(tempStack.Pop());
 150                    }
 51
 1952                    break;
 53                }
 4454            }
 2855        }
 56
 357        var result = new char[stack.Count];
 58
 4059        for (var i = result.Length - 1; i >= 0; i--)
 1760        {
 1761            result[i] = stack.Pop();
 1762        }
 63
 364        return new string(result);
 365    }
 66}