

For the given string “[X)](X”, the possible replacement for the first X is ‘(‘ and the second X is ‘)’, which makes the sequence “[()]()”, which is a valid balanced sequence.
For the given string “[XX{”, there is no possible replacement for X which can make it a valid bracket sequence.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first and only line of each test case or query contains the string.
For each test case, print “Valid” if a balanced sequence is possible, otherwise, print “Invalid”, without quotes in a separate line.
You do not need to print anything, it has already been taken care of. Just return 'True' for valid string and 'False' for an invalid string.
1 <= T <= 10
1 <= N <= 20
Time limit: 1 sec
The idea is to use stack data structure and is pretty much similar to the algorithm where we check if the expression is balanced or not.
We mainly have 3 conditions to fulfil i.e.
Let us work on example “X(X)”
Initially, stack s = { }
Let the function definition be isValidHelper(String str, stack s, ele) where ele is the index of the current character on which we are working, initially,ele = 0.