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
- 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
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- 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
- Print all ways to break a string in bracket form
- Construct Binary Tree from String with bracket representation
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Convert an unbalanced bracket sequence to a balanced sequence
- Find if an expression has duplicate parenthesis or not
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.