Grokking the Coding Interview: Patterns for Coding Questions
Vote
0% completed
Introduction to Stack Pattern
You are given a string of brackets. Decide whether every bracket is closed by the right partner, in the right order.
{[()]} is balanced
{[(])} is not
One approach is to scan the string again and again, deleting any adjacent matching pair each time, until nothing changes. If the string becomes empty it was balanced. That works, and it can cost O(N²) because each pass may remove only one pair.
Look at what the question actually needs. When a closing bracket arrives, only one opening bracket can possibly match it: the most recent one that is still open
.....
.....
.....
Like the course? Get enrolled and start learning!