The problem with brackets

Report a typo

Here is one of the classic tasks of computer science — check for paired brackets. You get a string with brackets. It can be a correct layout like (())() or a string with missing brackets ())() (the open bracket is missing). You need to find strings with broken layout and print out an error message. Try to solve it using a Queue structure.

Note that the Queue struct is already declared and set up to store rune values for this task.

Don't think about how to read the input and return an answer, focus on the task. If you are stuck, check out the following suggestions:

  1. If it's an open bracket, Push it to the structure;

  2. If it's a closed bracket, Pop the value from the Queue structure;

  3. If the Pop function returns an error, it means the layout is broken;

  4. In the end, you need to Pop the last value;

  5. If the last value exists, it means the layout is broken;

  6. If the structure is empty, it means the layout is correct.

Sample Input 1:

(())()

Sample Output 1:

All brackets are closed!
Write a program in Go
package main

import (
"fmt"
"errors"
)

func main() {
var brackets string
// the Queue struct is hidden but already declared
var solver Queue

fmt.Scan(&brackets)

for _, bracket := range brackets {
if bracket == '(' {
?
}
if bracket == ')' {
?
if ? {
fmt.Println("error")
return
}
}
}

?
if ? {
fmt.Println("All brackets are closed!")
} else {
fmt.Println("error")
}
}
___

Create a free account to access the full topic