Bracket Match Problem(括号匹配问题)

Posted Dream_it_possible!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bracket Match Problem(括号匹配问题)相关的知识,希望对你有一定的参考价值。

Question

        Given a string, which is combined with '(',')''[',']','{','}', please follow the rules below to determine weather the string is matched.

example

one:

Input:  "()"

Output: true

two:

Input: "(())[][]"

Output: true

three:

Input:  "()([]"

Output: false

Thinking 

        If you observe carefully, then you will find every pair of parentheses are symmetrical, it is a good choice to use statck to help us solve this problem, why? 

        Stack has a first-in first-out feature, so we can push left bracket in the stack, and we could get a compare when get the right bracket, if matched until the end of the loop, return true, otherwise return false.

        once the loop is over, we can judge whether it meets the requirement according to the size of stack, if  the size of stack is zero, return true, otherwise, return false.

package leetcode100;

import java.util.Stack;

/**
 * @author bingbing
 * @date 2021/8/19 0019 14:09
 * 括号匹配问题。
 * Given a string, which is combined with '(',')''[',']','{','}',
 * please follow the rules below to determine whether the string is matched
 * example:
 * Input: "()"
 * Output: true
 * <p>
 * Input:"(())[][]"
 * Output: true
 * <p>
 * Input:"()([]"
 * Output:false
 */
public class BracketMatchProblem {


    public static void main(String[] args) {

        String str = "(){}[][][]()";
        boolean match = matchBracketProblem(str);
        System.out.println("The brackets matched id :" + match);

    }

    private static boolean matchBracketProblem(String str) {

        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else {
                if (!stack.isEmpty()) {
                    Character element = stack.pop();
                    char left = '(';
                    switch (c) {
                        case ')':
                            left = '(';
                            break;
                        case ']':
                            left = '[';
                            break;
                        case '}':
                            left = '{';
                            break;
                        default:
                            break;
                    }
                    if (left != element) {
                        return false;
                    }
                } else {
                    return true;
                }
            }
        }

        if (stack.size() == 0) {
            return true;
        }
        return false;
    }
}

 Test

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

Output: true

 

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

Output: false 

 

 

以上是关于Bracket Match Problem(括号匹配问题)的主要内容,如果未能解决你的问题,请参考以下文章

CF 612C. Replace To Make Regular Bracket Sequence括号匹配

E. Almost Regular Bracket Sequence

Gym 240084E - Correct Bracket Sequence Editor - [线段树]

UVALive-8078 Bracket Sequence 简单dp

Codeforces Round #605 (Div. 3) F. Two Bracket Sequences 三维dp

VScode 4 括号颜色分级插件(Bracket Pair Colorizer)