Array ( [0] => [1] => questions [2] => Basic [3] => String-Of-Brackets ) Codevita Previous Questions | String Of Brackets | THE INQUISITIVE





String Of Brackets

LEVEL:Beginner

Description

CODU loves to play with string of brackets.
He considers string as a good string if it is balanced with stars. A string is considered as balanced with stars if string contains balanced brackets and between every pair of bracket i.e. between opening and closing brackets, there are at least 2 stars(*) present.
CODU knows how to check whether a string is balanced or not but this time he needs to keep a track of stars too. He decided to write a program to check whether a string is good or not. But CODU is not as good in programming as you are, so he decided to take help from you. Will you help him for this task?
You need to print Yes and number of balanced pair if string satisfies following conditions(string is good if it satisfies following 2 conditions):
The string is balanced with respect to all brackets.
Between every pair of bracket there is at least two stars.
However if string doesn't satisfies above conditions then print No and number of balanced pair in string as an output.

Input Format

The first and only line of input contains a string of characters(a-z,A-Z), numbers(0-9), brackets( '{', '[', '(', ')', ']', '}' ) and stars(*).
Constraints
4 <= String length <= 1000

Output Format

Print space separated 'Yes' (without quotes) and number of balanced pair if string is good. Else print 'No' (without quotes) and number of balanced pair.


Example 1:

Input
{**}
Output
Yes 1
Example 2:

Input
{**(**{**[**]})}
Output
Yes 4
Example 3:

Input
**}xasd[**]sda231
Output
No 1

oops

Login to see Discussion




Approach

Maintaining a stack to track the brackets. Iterate through the string and PUSH the bracket if it's an open bracket and POP the bracket if its closed bracket. Check for the consecutive '*' in the string, if there are consecutive '*' increase the count. At last check if the stack is empty, print 'Yes' and the count, else print 'No' and the count.


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution