1. 程式人生 > >678. Valid Parenthesis String

678. Valid Parenthesis String

exce IV charat respond 思路 types clas tar define

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;
    }

    

678. Valid Parenthesis String