0

Need an efficient algorithm

Hello , please help me to determine an efficient algorithm to solve this problem ... Given a string which contains some characters . It is guaranteed that the string includes at least one pair of open and close brackets . I need to make a program to output , which open bracket belongs to which close bracket ? Input : --[--]- Output : 2 5 Input : --[--[--[]--[-]-]---]--- Output : 2 20 5 16 8 9 12 14

19th May 2021, 3:22 PM
Ali_combination
Ali_combination - avatar
3 Respostas
+ 3
use one array, push current index at end of it when encountered an opening bracket, and pop last index stored when encountered a closing bracket: you've then got the pair of bracket indices (current, last) ^^
19th May 2021, 3:32 PM
visph
visph - avatar
+ 2
obviously, you need to sort the pairs if you want them in order ;P
19th May 2021, 3:46 PM
visph
visph - avatar
0
visph So I just need to implement it so perfectly 🤷‍♂️ while I thought that is not the best algorithm ... I thought there should be a more creative solution ... . Ok thanks .
19th May 2021, 3:43 PM
Ali_combination
Ali_combination - avatar