• 678. Valid Parenthesis String


    678. Valid Parenthesis String

    Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules:

    1. Any left parenthesis '(' must have a corresponding right parenthesis ')'.
    2. Any right parenthesis ')' must have a corresponding left parenthesis '('.
    3. Left parenthesis '(' must go before the corresponding right parenthesis ')'.
    4. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
    5. An empty string is also valid.

    Example 1:

    Input: "()"
    Output: True
    

    Example 2:

    Input: "(*)"
    Output: True
    

    Example 3:

    Input: "(*))"
    Output: True
    

    Note:

    1. The string size will be in the range [1, 100].

      看到题目后,有印象曾经也在leetcode做过括号配对的题目,但是这题比较难处理的是‘*’号,作为任意匹配字符,既可以消耗一个‘)’号,也可以作为无用字符。思考一会后,基本确定思路:

        1、使用stack,入栈‘(’和‘*’号,在遇到‘)'时,出栈(’和‘*’号;

        2、出栈过程中优先消耗左括号,实在没有对应的左括号时,消耗一个离栈顶最近的星号;

        3、从栈顶开始找左括号的过程中,如果找到对应的做左括号,要把寻找过程中出栈的星号再次压回栈中。

      具体代码如下,要注意栈中无元素的情况下,继续出栈,会报ArrayIndexOutOfBoundsException异常:

        public static void main(String[] args) {
            // String s = "((*)"
            String s = "(((******))";
            // String s = "(())((())()()(*)(*()(())())())()()((()())((()))(*";
            System.out.println(checkValidString(s));
        }
    
        public static boolean checkValidString(String s) {
            Stack<Character> chars = new Stack<>();
            int countStart = 0;
            for (int i = 0; i < s.length(); i++) {
                char curr = s.charAt(i);
                if ('(' == curr || '*' == curr) {
                    chars.push(curr);
                } else if (')' == curr) {
                    if (chars.size() == 0) {
                        return false;
                    }
                    countStart = 0;
                    while (!chars.isEmpty() && chars.peek() == '*') {
                        chars.pop();
                        countStart++;
                    }
                    if (!chars.isEmpty()) {
                        chars.pop();
                        while (countStart-- > 0) {
                            chars.push('*');
                        }
                    } else if (countStart > 0) {
                        int temp = countStart - 1;
                        while (temp-- > 0) {
                            chars.push('*');
                        }
                    } else {
                        return false;
                    }
                }
            }
            if (chars.isEmpty()) {
                return true;
            } else {
                countStart = 0;
                while (!chars.isEmpty()) {
                    char a = chars.pop();
                    if ('(' == a) {
                        if (countStart > 0) {
                            countStart--;
                        } else {
                            return false;
                        }
                    } else {
                        countStart++;
                    }
                }
            }
            return true;
        }
    
        
  • 相关阅读:
    在oschina上新建项目的步骤
    将txt转为DataTable的方法
    设置IIS让网站拥有“网站目录外文件”的读写权限的操作(图文)
    从客户端****中检测到有潜在危险的 Request.QueryString 值在.net mvc下的解决方法
    动态调用类里的方法的示例(wjx)
    Pyhton忽略返回变量方法
    wsl安装Ubuntu16.04+Python2.7
    win10快速调用Shell代替GitBash
    wsl与win10文件互访
    OpenCV报错file too short解决
  • 原文地址:https://www.cnblogs.com/lyInfo/p/9126045.html
Copyright © 2020-2023  润新知