Valid Parentheses
Updated
Problem statement
Given a string s made only of the characters '(', ')', '[', ']', '{' and '}', return true if s is valid and false otherwise.
A string is valid when all of these hold:
- Every opening bracket is closed by a closing bracket of the same kind.
- Brackets close in the right order: a closing bracket must match the most recent opening bracket that is still open.
- Every closing bracket has a matching opening bracket before it.
Function
isValid(s: String) → booleanExamples
Example 1
s = "{[]()}"return = trueThe pairs [] and () each close right away, and then } closes the { that holds them both.
Example 2
s = "[(])"return = falseWhen ] arrives, the most recent open bracket is (, not [, so the brackets close in the wrong order.
Example 3
s = "(()"return = falseThe second ( is closed by ), but the first ( is never closed.
Example 4
s = "()]{}"return = falseAfter () closes, nothing is open, so the ] at index 2 has no opening bracket to match.
Constraints
1 <= s.length <= 104sconsists only of the characters'(',')','[',']','{'and'}'.
More Zillow Phone Screen questions
- Build a Hash TableOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- First Unique Character in a StringOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
- Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
- First N Fibonacci NumbersOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2021
- Lowest Common Ancestor in a Binary TreePHONE SCREEN · Seen Sep 2019
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Blackjack Hand ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2016
- FactorialPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2014
- Find the Four Missing Numbers in a FileOA · PHONE SCREEN · Seen Feb 2014
Similar questions at other companies
- Valid ParenthesesByteDance · PHONE SCREENByteDance interview questions
- Valid ParenthesesCisco · PHONE SCREENCisco interview questions
- Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Valid ParenthesesEPAM · ONSITE INTERVIEWEPAM interview questions
- Valid ParenthesesQualys · ONSITE INTERVIEWQualys interview questions
- Validate a Bracket SequenceTripAdvisor · PHONE SCREEN
- Rich Text Parser Part 1 (Validate Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Valid ParenthesesOracle · PHONE SCREENOracle interview questions
- Valid ParenthesesAmazon · PHONE SCREEN · ONSITE INTERVIEW