Marvell Technology Interview Question

Given an expression with brackets, Suggest a solution to check legality: Examples: ((), )(), (((} , {[()]