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:
- Count pairs of parentheses sequences such that parentheses are balanced
- Length of Longest Balanced Subsequence
- Print all combinations of balanced parentheses
- Pairs involved in Balanced Parentheses
- Check for balanced parentheses in an expression
- Check for balanced parentheses in Python
- Check for balanced parentheses in an expression | O(1) space
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Find the number of valid parentheses expressions of given length
- Longest prefix which is also suffix
- Longest Common Prefix using Sorting
- Longest Common Prefix using Trie
- Longest Common Prefix Matching | Set-6
- 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 firstname.lastname@example.org. 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.