Given a string of open bracket ‘(‘ and closed bracket ‘)’. The task is to find the length of longest balanced prefix.
Input : S = "((()())())((" Output : 10 From index 0 to index 9, they are forming a balanced parentheses prefix. Input : S = "()(())((()" Output : 6
The idea is take value of open bracket ‘(‘ as 1 and value of close bracket ‘)’ as -1. Now start finding the prefix sum of the given string. The farthest index, say maxi, where the value of sum is 0 is the index upto which longest balanced prefix exists. So the answer would be maxi + 1.
Below is the implementation of this approach:
- Length of Longest Balanced Subsequence
- Check for balanced parentheses in an expression
- Check for balanced parentheses in Python
- Print all combinations of balanced parentheses
- Pairs involved in Balanced Parentheses
- Check for balanced parentheses in an expression | O(1) space
- Count pairs of parentheses sequences such that parantheses are balanced
- Find the number of valid parentheses expressions of given length
- Longest prefix which is also suffix
- Longest Common Prefix using Trie
- Longest Common Prefix Matching | Set-6
- Longest Common Prefix using Sorting
- Number of balanced bracket subsequence of length 2 and 4
- Longest Common Prefix using Linked List
- Longest Common Prefix using Binary Search
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.