LeetCode 678. Valid Parenthesis String 有效的括号字符串 (C++/Java)

题目:

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

分析:

括号匹配,左括号和右括号必须对应上,且左括号在右括号前面,*号可以当任意的左括号右括号或者是空。

首先可以利用动态规划,求解dp[i][j],其中i,j表示字符串的字串范围,为1表示可以匹配上,0表示不能匹配上。当i等于j是,也就是一个字符,只有当时‘*’的时候才能匹配,那如果s[i]是*或者是左括号,s[j]是*或者右括号,且dp[i+1][j-1]是成功匹配的,那么dp[i][j]也是匹配的。否则就要在i到j之间寻找一个分割点k使得i到k,k+1到j之间的字符串是匹配的。

那还可以利用计数法,设置两个变量,一个记录当前需要匹配的最小左括号数,和一个当前需要匹配最大左括号数。

遍历字符串,当前字符为左括号时,意味着我们必须要匹配一个左括号,所以最小左括号数+1,其他情况最小左括号数减一,因为*可以当成右括号,消掉一个左括号。

当前字符为右括号时,意味着我们必须要匹配一个右括号,此时最大左括号数-1,其他情况最大左括号数+1,因为*可以当成左括号,增加一个最大的左括号数。

当最大左括号数小于0时,代表已经有右括号没有相应的左括号或者*和它匹配了,如果最小左括号数小于0,则重新将它置为0,因为最小左括号数小于0的情况时发生在有多个*出现,我们把*当成空,所以置其为0.最后如果最小括号数为0的话,就代表匹配成功了。

程序:

C++

class Solution {
public:
    bool checkValidString(string s) {
        int n = s.length();
        if(n == 0) return true;
        dp = vector<vector<int>>(n, vector<int>(n, -1));
        return checkValid(s, 0, n-1);
    }
private:
    vector<vector<int>> dp;
    bool checkValid(string& s, int i, int j){
        if(i > j)
            return true;
        if(dp[i][j] >= 0)
            return dp[i][j];
        if(i == j){
            if(s[i] == ‘*‘){
                return dp[i][j] = 1;
            }
            else{
                return dp[i][j] = 0;
            }
        }
        if((s[i] == ‘*‘ || s[i] == ‘(‘) && (s[j] == ‘*‘ || s[j] == ‘)‘) && checkValid(s, i+1, j-1)){
            return dp[i][j] = 1;
        }
        for(int k = i; k < j; ++k){
            if(checkValid(s, i, k) && checkValid(s, k+1, j)){
                return dp[i][j] = 1;
            }
        }
        return dp[i][j] = 0;
    }
};

Java

class Solution {
    public boolean checkValidString(String s) {
        char[] str = s.toCharArray();
        if(str.length == 0) return true;
        int min_op = 0;
        int max_op = 0;
        for(char ch:str){
            if(ch == ‘(‘){
                min_op++;
            }else{
                min_op--;
            }
            if(ch == ‘)‘){
                max_op--;
            }else{
                max_op++;
            }
            if (max_op < 0) return false;
            min_op = Math.max(0, min_op);
        }
        if(min_op == 0)
            return true;
        else
            return false;
    }
}

原文地址:https://www.cnblogs.com/silentteller/p/12348010.html

时间: 2024-08-30 04:52:23

LeetCode 678. Valid Parenthesis String 有效的括号字符串 (C++/Java)的相关文章

678. Valid Parenthesis String

/* 一开始想的是双指针,从两边开始检测,如果有*或者匹配就前进,但是想了想不对,因为可能此时不匹配,但是前边会有*来配对, 或者此时的*不知道应该当做什么,因为会对前边的有影响. 由于*和(会对后边的有影响,所以要把坐标存起来 */ if (s.length()==0) return true; int l = s.length(); Stack<Integer> le = new Stack<>(); Stack<Integer> st = new Stack<

【LeetCode】- Valid Parentheses(有效的括号)

[ 问题: ] Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 直译:给定一个字符串,该串包含字符'(', ')', '{', '}', '[', ']', 请判断它是不是有效的 The brackets must close in the correct order, "()" and "

[LeetCode] Longest Valid Parentheses 最长有效括号

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac

Leetcode 678.有效的括号字符串

有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何右括号 ) 必须有相应的左括号 ( . 左括号 ( 必须在对应的右括号之前 ). * 可以被视为单个右括号 ) ,或单个左括号 ( ,或一个空字符串. 一个空字符串也被视为有效字符串. 示例 1: 输入: "()" 输出: True 示例 2: 输入: "(*)" 输出: True 示

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen

[LeetCode] 020. Valid Parentheses (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 020.Valid_Parentheses (Easy) 链接: 题目:https://oj.leetcode.com/problems/valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个括号字符串是否是有效的. 分析: