Matching brackets
Write code · Medium · §5.1 Stacks
The string s is made up only of parentheses (, ) and square brackets [, ]. The brackets match if each opening bracket is closed later by a closing bracket of the same shape, the pairs are properly nested, and no closing bracket appears without an opening bracket before it. Complete the function solution, which returns True if the brackets match and False otherwise.
For example, the brackets in "([])" match, but the brackets in "([)]" do not.
Function
solution(s)
Constraints
- The length of
sis from 1 to 100,000.
Examples
| s | result |
|---|---|
'([])' | True |
'([)]' | False |
Starter code
def solution(s):
answer = False
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.