[Leetcode] 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].

思路

1、回溯法:我首先想到的是回溯法:每个'*'有三种可能,我们分别进行枚举。需要保证在任何时候,左括弧的数量都大于等于右括弧的数量,并且最终两者相等。请见下面的代码。虽然回溯法可以通过所有测试,但是一旦'*'的数量很多,则算法的时间复杂度就变得很高(假设有n个‘*’,则需要枚举3^n种可能性)。

2、范围法:这是在网上看到的更巧妙的一种方法:我们记录'('没有被')’match的范围[lower, upper]。任何时刻,lower都应该大于等于0,upper也应该大于等于0,并且最终lower需要等于0。需要注意的是,任何时刻如果lower小于0并不需要返回false,因为这种减少有可能是因为‘*’的出现而导致的,此时只需要修补一下,让lower大于等于0即可(意味着在某些地方'*'不能被当做‘)’,而只能当做'('来用);但是upper在任何时候都不能小于0,因为这意味着该时刻‘)’超过了‘(’和‘*’的总和。最后检查lower是否等于0即可。

代码

1、回溯法:

class Solution {
public:
    bool checkValidString(string s) {
        return checkValidString(s, 0, 0, 0);
    }
private:
    bool checkValidString(string &s, int pos, int left_num, int right_num) {
        if (left_num < right_num) {
            return false;
        }
        if (pos == s.length()) {
            return left_num == right_num;
        }
        if (s[pos] == '(') {
            return checkValidString(s, pos + 1, left_num + 1, right_num);
        }
        else if (s[pos] == ')') {
            return checkValidString(s, pos + 1, left_num, right_num + 1);
        }
        else {
            return checkValidString(s, pos + 1, left_num, right_num + 1) ||
                   checkValidString(s, pos + 1, left_num, right_num) ||
                   checkValidString(s, pos + 1, left_num + 1, right_num);
        }
    }
};

2、范围法:

class Solution {
public:
    bool checkValidString(string s) {
        int lower = 0, upper = 0;
        for (char c : s) {
            if (c=='(') {
                lower++;
                upper++;
            } else if (c==')') {
                lower--;               
                upper--;
            } else {                // * encountered
                lower--;
                upper++;
            }
            lower = max(lower, 0);  // lower <= 0 means we have unmatched ')'
            if (upper < 0) {        // unmatched ')' found in the middle of string
                return false;
            }
        }
        return lower == 0;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值