LeetCode primary algorithm - Other 02: Effective brackets

LeetCode primary algorithm - Other 02: Effective brackets

Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
CSDN: HTTPS: //blog.csdn. NET / abcgkj /
GitHub: https://github.com/aimi-cn/AILearners

First, the primer

It was launched by the LeetCode official list of interview questions classics -
this module is to explore the corresponding primary algorithm - designed to help entry-algorithm. Our first pass leetcode brush is recommended topics.
View the complete algorithm Offer to prove safety issues resolved, please click on the link github:
github address

Second, the title

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

Valid string must meet:

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

Note the empty string can be considered a valid string.

Example 1:

输入: "()"
输出: true

Example 2:

输入: "()[]{}"
输出: true

Example 3:

输入: "(]"
输出: false

Example 4:

输入: "([)]"
输出: false

Example 5:

输入: "{[]}"
输出: true

1, ideas

We observed examples of valid strings meet a few examples of difficult to find the correct string for it every time remove a pair of parentheses, and finally became empty -

2, programming

python

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        while '[]' in s or '{}' in s or '()' in s:
            s = s.replace('[]','')
            s = s.replace('{}','')
            s = s.replace('()','')
        return s == ''

AIMI-CN AI learning exchange group [1015286623] for more information on AI

Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!

This article from the blog article multiple platforms OpenWrite release!

Guess you like

Origin www.cnblogs.com/aimi-cn/p/11760002.html