solution for bracket sequence problem