# Valid Parentheses

Given a string containing just the characters

> '(', ')', '{', '}', '\[' and ']'

determine if the input string is valid.

The brackets must close in the correct order,

> "()" and "()\[]{}"

are all valid but

> "(]" and "(\[)]"

are not.

{% tabs %}
{% tab title="Scala" %}

```scala
val brackets:Map[Char, Char] = Map[Char, Char] (
  ')' -> '(',
  '}' -> '{',
  ']' -> '['
)

def isValid(s: String): Boolean = {
  var lst: List[Char] = Nil
  for (c <- s) {
    if (brackets contains c) {
      for (open <- brackets.get(c)) {
        if (lst.head == open) lst = lst.tail
        else return false
      }
    } else {
      lst = c :: lst
    }
  }
  lst.isEmpty
}

isValid("()")
isValid("()[]{}")
isValid("(]")
isValid("([)]")
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.bernardw.com/problems/valid-parentheses.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
