< Summary

Information
Class: LeetCode.Algorithms.ValidParentheses.ValidParenthesesStackDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidParentheses\ValidParenthesesStackDictionary.cs
Line coverage
100%
Covered lines: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 46
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
IsValid(...)87.5%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidParentheses\ValidParenthesesStackDictionary.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.ValidParentheses;
 13
 14/// <inheritdoc />
 15public class ValidParenthesesStackDictionary : IValidParentheses
 16{
 517    private readonly Dictionary<char, char> _parenthesesDictionary = new() { { ')', '(' }, { '}', '{' }, { ']', '[' } };
 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 bool IsValid(string s)
 526    {
 527        var parenthesesStack = new Stack<char>();
 28
 5829        foreach (var c in s)
 2230        {
 2231            if (_parenthesesDictionary.TryGetValue(c, out var value))
 1132            {
 1133                if (parenthesesStack.Count == 0 || parenthesesStack.Pop() != value)
 134                {
 135                    return false;
 36                }
 1037            }
 38            else
 1139            {
 1140                parenthesesStack.Push(c);
 1141            }
 2142        }
 43
 444        return parenthesesStack.Count == 0;
 545    }
 46}

Methods/Properties

.ctor()
IsValid(System.String)