Since the last bracket that is opened must also be the first one to be closed, it makes sense to use a data structure that uses the Last In, First Out (LIFO) principle. Therefore, a stack is a good choice here.
Method 2 - Using Counter When Only 1 Type of Parenthesis Present#
1
2
3
4
5
6
7
8
9
10
11
12
Boolean: IsProperlyNested(String: expression)
Integer: counter = 0
For Each ch In expression
If (ch =='(') Then counter = counter + 1
Else If (ch ==')') Then
counter = counter - 1
If (counter < 0) Then Return False
End If
Next ch
If (counter == 0) Then Return True
Else Return False
IsProperlyNested