题目:

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

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

  1. [leetcode]678. Valid Parenthesis String验证有效括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  2. leetcode 678. Valid Parenthesis String

    678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...

  3. [LeetCode] 678. Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  4. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  5. 678. Valid Parenthesis String

    https://leetcode.com/problems/valid-parenthesis-string/description/ 这个题的难点在增加了*,*可能是(也可能是).是(的前提是:右边 ...

  6. 【leetcode】Valid Parenthesis String

    题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...

  7. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  8. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  9. [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  10. LeetCode Valid Parenthesis String

    原题链接在这里:https://leetcode.com/problems/valid-parenthesis-string/description/ 题目: Given a string conta ...

随机推荐

  1. 力扣121(java&python)-买卖股票的最佳时机(简单)

    题目: 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格. 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票.设计一 ...

  2. 可观测|时序数据降采样在Prometheus实践复盘

    简介: 基于 Prometheus 的监控实践中,尤其是在规模较大时,时序数据的存储与查询是其中非常关键,而且问题点较多的一环.如何应对大数据量下的长周期查询,原生的 Prometheus 体系并未能 ...

  3. [Cryptocurrency] (XMR) Monero GUI 连接远程节点 操作方式

    Monero 官网下载的钱包,在 高级设置 的节点里支持 "本地节点" 和 "远程节点". 本地节点就是同步区块链数据到本地电脑,安全性高,占用空间大. 远程节 ...

  4. [Nova] belongsTo, belongsToMany 当前页动态 dependsOn 其它 fields, nova-belongs-to-dependency, belongs-to-many-field-nova

    nova-belongs-to-dependency 例子: use Manmohanjit\BelongsToDependency\BelongsToDependency; ... return [ ...

  5. WPF 框架开发 调试和开发 XAML 构建过程的 PresentationBuildTasks 方法

    阅读本文,你可以了解如何编写开发和调试 XAML 构建为 Baml 和 g.cs 文件的过程和工具.本文也适合想要了解 WPF 的 XAML 构建过程的开发者阅读,本文提供了可以断点调试 WPF 的 ...

  6. 《MySql必知必会》笔记整理

    数据库基础 关键词: 数据库 表(表名唯一,取决多个因素,如不同数据库的表可以同名) 模式(关于数据库和表的布局及特性的信息) 列(表中的字段) 行[行(raw)和记录(record)很大程度可以等同 ...

  7. aspnetcore插件开发dll热加载

    该项目比较简单,只是单纯的把业务的dll模块和controller的dll做了一个动态的添加删除处理,目的就是插件开发.由于该项目过于简单,请勿吐槽.复杂的后续可以通过泛型的实体.dto等做业务和接口 ...

  8. Oracle、达梦:_ 英文下划线 让LIKE查询失效的解决方案:ESCAPE关键字

    oracle/dm:_ 英文下划线让like查询失效的解决方案:ESCAPE关键字 -- 可以查询出带(\)的值 SELECT "f1","f2" FROM & ...

  9. telegraph + influxdb + grafana 实现交换机流量展示

    实验环境 influxdb2:2.7.5 telegraf:1.30.1 grafana:10.4.2 influxdb 官方文档见https://docs.influxdata.com/influx ...

  10. docker / compose 的安装 和 体验

    文档 官网文档 视频 视频 简介 课程内容 1.Docker Compose 容器编排 2.Docker Swarm #集群 热扩容 需要在阿里上买服务器,至少冲100+以上的人民币 文档: 集群方式 ...