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
- Expression contains redundant bracket or not
- 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
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Construct Binary Tree from String with bracket representation
- Print all ways to break a string in bracket form
- Find if an expression has duplicate parenthesis or not
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Find last index of a character in a string
- Find i'th Index character in a binary string obtained after n iterations
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.