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
3 odpowiedzi
+ 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) ^^
+ 2
obviously, you need to sort the pairs if you want them in order ;P
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 .