LeetCode Brush question - Effective brackets (simple)

Title Description

Includes only a given '(', ')' string, '{', '}', '[', ']', and determine whether the string is valid.
Valid string must meet:

  • Left bracket must be closed by a closing parenthesis of the same type.
  • Left parenthesis must be closed in the correct order.

Note the empty string can be considered a valid string.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "() [] {}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

 

Simplified version

Let's look at a simplified version of the problem in question after simplification, containing only one type of brackets. This way, we will encounter is expression

( ( ( ( ( ( ) ) ) ) ) )   - VALID
( ) ( ) ( ) ( )               -VALID
( ( ( ( ( ( ( ( )          -INVALID
( ( ( ) ( ( ) ) ) )       - VALID

We tried to use a simple algorithm to solve this problem.

 

  1. We start from the left side of the expression, handle only a parenthesis.

  2. Suppose we encounter an open parenthesis, ie ( expression is invalid or not depends on whether there are other parts of this expression that matches the closing parenthesis that is  ) . At this point, we just increment the counter to keep track of the number of open brackets until now. left + = 1.

  3. If we encounter a closed parenthesis, this could mean two such cases:

    ·   This no corresponding closing parenthesis open parenthesis, in this case, our expression is invalid. When left == 0, i.e. there are no unpaired available left parenthesis is the case.

    ·   We have some unpaired opening bracket can be paired with the closing parenthesis. When 1eft> 0, i.e. when available unpaired left parenthesis is the case.


  4. If we encounter a closed parenthesis in the left == 0 for example  ) , then the current expression is invalid. Otherwise, we will reduce the value of the left, that is, reducing the number of available unpaired left parenthesis.

  5. Continue processing strings, until all have been processed brackets.

  6. Finally, if we still have left parenthesis unpaired, which means that expression is invalid.

In this particular algorithm is discussed here because we get inspiration from this solution to solve the original problem. To better understand the algorithm of our discussion, please watch the following animation.

 

If our approach to the original question, this is simply not feasible. Based on a simple counter in the above method can perfectly because all brackets are of the same type.

Thus, when we encounter a closing bracket, we need only assume that there is a match a corresponding open bracket is available, i.e., assuming left> 0.

However, in our problem, if we meet  ], we really do not know whether there is a corresponding [  available. You might ask:

Why not for different types of brackets are to maintain a separate counter?

This may not work, because the relative position of the brackets here is also very important. E.g:

[ { ]

如果我们只是在这里维持计数器,那么只要我们遇到闭合方括号,我们就会知道此处有一个可用的未配对的开口方括号。

但是,最近的未配对的开括号是一个花括号,而不是一个方括号,因此计数方法在这里被打破了。

方法一:栈

关于有效括号表达式的一个有趣属性是有效表达式的子表达式也应该是有效表达式。(不是每个子表达式)例如

 

 

 

 

Guess you like

Origin www.cnblogs.com/xiaozhongfeixiang/p/12085035.html
Recommended