m × n grid grid where every cell holds a bracket, ( or ). A route starts at the top-left cell, ends at the bottom-right cell and moves either right or down at every step. Writing down the brackets of its cells in order gives a string.
A bracket string is *balanced* if every opening bracket can be paired with a closing bracket after it so that the pairs do not cross: (())() is balanced, while )( and (() are not.
Return True if some route produces a balanced string, and False otherwise.grid=[['(', '(', ')'], [')', '(', ')']] → TrueGoing right, right, down collects (()), a balanced string.
grid=[['(', '(', ')'], ['(', ')', '(']] → FalseEvery route ends on a cell with (, and a balanced string cannot end with an opening bracket.
grid=[['(', ')', '('], [')', '(', ')'], ['(', ')', ')'], [')', '(', ')']] → True( or )grid=[['(', '(', ')'], [')', '(', ')']]True
Run checks the first 3 tests in your browser. Submit checks all 42 tests on the server, hidden ones included.