ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

T-SQL: Check for Balanced Parentheses

T-SQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE FUNCTION dbo.IsBalancedParentheses (@Expression NVARCHAR(MAX))
2RETURNS BIT
3AS
4BEGIN
5DECLARE @Stack TABLE (Char NCHAR(1));
6DECLARE @i INT = 1;
7DECLARE @Len INT = LEN(@Expression);
8
9IF @Len = 0 RETURN 1;
10
11WHILE @i <= @Len
12BEGIN
13DECLARE @CurrentChar NCHAR(1) = SUBSTRING(@Expression, @i, 1);
14
15IF @CurrentChar IN ('(', '[', '{')
16BEGIN
17INSERT INTO @Stack (Char) VALUES (@CurrentChar);
18END
19ELSE IF @CurrentChar IN (')', ']', '}')
20BEGIN
21IF NOT EXISTS (SELECT 1 FROM @Stack)
22BEGIN
23RETURN 0; -- Closing delimiter with no matching opening delimiter
24END
25
26DECLARE @LastOpen NCHAR(1);
27SELECT TOP 1 @LastOpen = Char FROM @Stack ORDER BY (SELECT NULL) DESC;
28
29IF (@CurrentChar = ')' AND @LastOpen = '(') OR
30(@CurrentChar = ']' AND @LastOpen = '[') OR
31(@CurrentChar = '}' AND @LastOpen = '{')
32BEGIN
33DELETE FROM @Stack WHERE Char = @LastOpen;
34END
35ELSE
36BEGIN
37RETURN 0; -- Mismatched delimiters
38END
39END
40SET @i = @i + 1;
41END
42
43RETURN CASE WHEN NOT EXISTS (SELECT 1 FROM @Stack) THEN 1 ELSE 0 END;
44END
Algorithm description viewbox

T-SQL: Check for Balanced Parentheses

Algorithm description:

This T-SQL function checks if a string containing various types of parentheses, brackets, and braces has correctly matched and ordered delimiters. It simulates a stack data structure using a table variable. Opening delimiters are pushed onto the stack, and closing delimiters attempt to pop their corresponding opening delimiter. If at any point a closing delimiter is encountered without a matching opening one on top of the stack, or if the stack is not empty at the end, the string is considered unbalanced.

Algorithm explanation:

The function iterates through the input string character by character. Opening delimiters ('(', '[', '{') are pushed onto a simulated stack (a table variable). When a closing delimiter (')', ']', '}') is encountered, the function checks if the stack is empty (indicating an unbalanced string) or if the top element of the stack is the corresponding opening delimiter. If it matches, the opening delimiter is popped from the stack. If it doesn't match or the stack is empty, the string is unbalanced. After processing the entire string, if the stack is empty, all delimiters were balanced; otherwise, there are unmatched opening delimiters. The time complexity is O(N) where N is the length of the string, as each character is processed once. The space complexity is O(N) in the worst case, where the string consists entirely of opening delimiters, requiring all to be stored on the stack. The invariant is that the stack always contains the sequence of unmatched opening delimiters encountered so far, in the order they appeared.

Pseudocode:

FUNCTION IsBalancedParentheses(expression):
  CREATE stack
  FOR each character in expression:
    IF character is an opening delimiter ('(', '[', '{') THEN
      push character onto stack
    ELSE IF character is a closing delimiter (')', ']', '}') THEN
      IF stack is empty THEN RETURN FALSE
      Pop the top element from stack
      IF the popped element does not match the current closing delimiter THEN RETURN FALSE

  IF stack is empty THEN RETURN TRUE
  ELSE RETURN FALSE