GitHub Repository Submission

When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more

Your GitHub repository URL is saved. LSBot will automatically fetch your latest code from this repository for each message. To change the URL, clear it first and save a new one.

Exercise 1

Determine whether to use a stack or a queue, and write an algorithm for the "Matching Brackets" problem.

You can use a Stack data structure to solve this problem.

Solution

For this problem, we'll maintain a stack as we iterate through the characters of the input string.

There are two distinct cases to consider based on the character type:

  1. Open Bracket: When the character is an open bracket ((, [, {), we push it onto the stack.
  2. Closed Bracket: When the character is a closed bracket (), ], }):
    • We attempt to pop the last character from the stack.
      • If the stack is empty, indicating there is no matching open bracket, we return false immediately.
      • If the current character and the popped character aren't a match return false.
      • If we've not returned false, we can continue iterating.

At the end of the process, if the stack is empty, all brackets are properly matched, and we can return true. Otherwise, if any brackets remain unmatched in the stack, we return false.


Walkthrough

Let's do a detailed walkthrough using a string "([()]{})".

Step 1: The first character is an open bracket, so we push it to the stack.

Step 2: Once again, the character is an open bracket, so we push it to the stack.

Step 3: In the third step we encounter an open bracket yet again.

Step 4: In the fourth step, the character is a closed bracket so we pop the last character from the stack and check if it matches the current character. Since the characters match, we continue.

Step 5: Once again, the character is a closed bracket so we pop the last character from the stack and check if it matches the current character. Since the characters match, we continue.

Step 6: The character is an open bracket, so we push it to the stack.

Step 7: The character is a closed bracket so we pop the last character from the stack and check if it matches the current character. Since the characters match, we continue.

Step 8: Finally, the last character is a closed bracket so we pop the last character from the stack and check if it matches the current character. The characters match, so we continue.

Step 9: We've reached the end of the string. We now check to see if the stack is empty. Since it is, we return true.


In the walkthrough, we've seen an example where all brackets matched correctly and the stack was empty at the end. Now, let's visually explore cases where this might not be true, and we'd have to return false instead.

Case 1: We have reached the end of the string, but the stack is not empty.

In this example, we would push the open bracket to the stack and successfully completed our iterations. At the end, however, our stack is not empty and we return false.


Case 2: We've encountered a closing bracket while the stack is empty.

Here, we encounter a closing bracket. That means we want to pop from the stack to compare with our closing bracket. In this case, the stack is empty, meaning we have a closing bracket with no matching opening bracket and we return false.

In the above example, we try to pop from an empty stack on the first iteration, but this isn't necessarily always the case. Consider what happens with the string '{()}]'.


Case 3: We've encountered a closing bracket that doesn't match the last character in the stack.

In our first step, we would push our first opening bracket onto the stack.

In the second step, we encounter a closing bracket. We successfully pop from the stack to get a character to compare against our bracket. When we make the comparison, however, they are not compatible brackets and we return false.

Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected. Want to know more? Refer to the LSBot User Guide .

Detected solution
Loading...

Submit your solution for LSBot review.

Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the LSBot User Guide .

Detected solution
Loading...