Balanced brackets
Coding problemEasy-MediumAsked in: Swiggy
Given a string s, return true if its brackets are balanced, and false otherwise. Three kinds of bracket count: round ( ), square [ ] and curly { }. Every other character is ignored.
Brackets are balanced when every opening bracket is closed by the same kind of bracket, closings come in the reverse order of openings, and no closing bracket appears without a matching opening one. A string with no brackets is balanced.
Examples
Example 1
Input: s = "{[()]}"
Output: true
Example 2
Input: s = "([)]"
Output: false
The square bracket opened second but is closed after the round one.
Example 3
Input: s = "f(x) = [a + b]"
Output: true
Constraints
- The length of s is between 0 and 100,000
- s holds printable ASCII characters only
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.
to answer and keep your progress. Reading is free for everyone.