Given a string with brackets. If the start index of the open bracket is given, find the index of the closing bracket.
Input : string = [ABC] index = 0 Output : 8 The opening bracket at index 0 corresponds to closing bracket at index 8.
The idea is to use Stack data structure. We traverse given expression from given index and keep pushing starting brackets. Whenever we encounter a closing bracket, we pop a starting bracket. If stack becomes empty at any moment, we return that index.
[ABC], 0: 8 [ABC], 4: 7 [ABC], 9: 12 [ABC], 1: -1
Time Complexity: O(n)
Auxiliary Space: O(n)
- Number of closing brackets needed to complete a regular bracket sequence
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Expression contains redundant bracket or not
- Check if expression contains redundant bracket or not | Set 2
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of bracket reversals needed to make an expression balanced
- Print Bracket Number
- Minimum Swaps for Bracket Balancing
- Number of balanced bracket subsequence of length 2 and 4
- Range Queries for Longest Correct Bracket Subsequence
- Construct Binary Tree from String with bracket representation
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Print all ways to break a string in bracket form
- Convert an unbalanced bracket sequence to a balanced sequence
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.