Given a valid parenthesis string S, the task is to find the weight of parenthesis based on the following conditions:
- Weight of “( )” is 1
- Weight of “AB” = weight of “A” + weight of “B” (where, A and B are both independent valid parenthesis). e.g. weight of “()()” = weight of “()” + weight of “()”
- Weight of “(A)” = 2 times weight of “A” (where, A is independent valid parenthesis). e.g. weight of “(())” is 2 times weight of “()”
Input: S = “()(())”
Weight of () = 1
Weight of (()) = 2
Hence, weight of ()(()) = 1 + 2 = 3
Input: S = “(()(()))”
Weight of ()(()) = 3
Weight of (()(())) = 2 * 3 = 6
This problem can be solved using Divide and Conquer approach. Follow the steps below to solve the problem:
- It is given that the input parenthesis string is always valid i.e. balanced. So, any opening bracket ‘(‘ has a corresponding closing bracket ‘)’.
- Consider the opening bracket at the beginning of the input string (The beginning bracket can not be a closing bracket, otherwise it will not be valid). Now, for this opening bracket, the corresponding closing bracket can have any of the following two possible indices.
- At the very end i.e. (n-1)th index
- Somewhere between start and end i.e. [1, n-2]
- If the closing bracket has an index at the end, then according to constraint no. 3, the total weight of parenthesis will be twice that of the weight of string[1, n-2].
- If the closing bracket is somewhere in between start and end, say mid, then according to constraint no. 2, the total weight of parenthesis will be the sum of the weight of string[start, mid] and the sum of the weight of string[mid+1, end].
- The base case for our recursion will be when we have only two brackets in the string, they will have weight 1 because inherently they will be valid.
- Now, the question is how we can find out the index of the corresponding closing bracket for an opening bracket. The idea is similar to Valid Parenthesis Check. We will use the Stack Data Structure to check and store the index of the closing bracket for the corresponding opening bracket in a HashMap.
Perform the following steps:
- Traverse through the string.
- If a character is an opening bracket, push its index into the Stack.
- If it is a closing bracket, pop its index from the Stack and insert the (popped_index, current_index) pairing into the HashMap.
Below is the implementation of the above approach.
Time Complexity: O(N)
Auxiliary Space: O(N), where N is the length of the string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct the Cypher string based on the given conditions
- Check for balanced parenthesis without using stack
- Reverse substrings between each pair of parenthesis
- Find if an expression has duplicate parenthesis or not
- Number of balanced parenthesis substrings
- Count all indices of cyclic regular parenthesis
- Identify and mark unmatched parenthesis in an expression
- Find maximum depth of nested parenthesis in a string
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Number of Paths of Weight W in a K-ary tree
- Change the given string according to the given conditions
- Expand the string according to the given conditions
- Count of Binary Strings possible as per given conditions
- Count of Nodes which has Prime Digit sum weight in a Tree
- Count the nodes in the given tree whose weight is a powerful number
- Count nodes in the given tree whose weight is a fibonacci number
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Number of strings in two array satisfy the given conditions
- Count pairs of strings that satisfy the given conditions
- Split the array into equal sum parts according to given conditions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.