题目:
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:
- Any left parenthesis
'('must have a corresponding right parenthesis')'. - Any right parenthesis
')'must have a corresponding left parenthesis'('. - Left parenthesis
'('must go before the corresponding right parenthesis')'. '*'could be treated as a single right parenthesis')'or a single left parenthesis'('or an empty string.- An empty string is also valid.
Example 1:
Input: "()" Output: True
Example 2:
Input: "(*)" Output: True
Example 3:
Input: "(*))" Output: True
Note:
- 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;
}
};
本文介绍了一种算法,用于判断包含括号及星号的字符串是否有效。通过两种方法——回溯法与范围法,实现了一个函数,确保括号匹配正确且星号灵活处理。文章详细解释了每种方法的工作原理,并提供了具体的代码实现。

被折叠的 条评论
为什么被折叠?



