题目:

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. Java面试题:请谈谈对ThreadLocal的理解?

    ThreadLocal是一种特殊的变量存储机制,它提供了一种方式,可以在每个线程中保存数据,而不会受到其他线程的影响.这种机制在多线程编程中非常有用,因为它允许每个线程拥有自己的数据副本,从而避免了数 ...

  2. 【笔记】Linux基础指令

    Linux基础指令 cd 跳转文件夹 cd 到根目录 cd usr 到根目录下的usr目录 cd .. 到上一级目录 cd ~ 到home目录 cd - 到上次访问的目录 sh 执行sh命令 ls 查 ...

  3. 力扣32(java)-最长有效括号(困难)

    题目: 给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度. 示例 1: 输入:s = "(()"输出:2解释:最长有效括号子串是 &quo ...

  4. 社区首款 OAM 可视化平台发布!

    作者 | 徐运元,杭州谐云科技合伙人及资深架构师,云计算行业和 Kubernetes 生态资深从业者 导读:什么是 OAM?2019 年 10 月 17 日,阿里巴巴合伙人.阿里云智能基础产品事业部总 ...

  5. Java异步非阻塞编程的几种方式

    简介: Java异步非阻塞编程的几种方式 一. 从一个同步的Http调用说起 一个很简单的业务逻辑,其他后端服务提供了一个接口,我们需要通过接口调用,获取到响应的数据. 逆地理接口:通过经纬度获取这个 ...

  6. 性能提升一倍!云原生网关支持 TLS 硬件加速

    简介:业界在优化 HTTPS 的性能上也做了诸多探索,传统的软件优化方案有 Session 复用.OCSP Stapling.False Start.dynamic record size.TLS1. ...

  7. 如何利用 AHAS 保障 Web 服务稳如磐石?

    ​简介:应用高可用服务 AHAS (Application High Availability Service) 是经阿里巴巴内部多年高可用体系沉淀下来的云产品,基于阿里开源流控降级组件 Sentin ...

  8. Multisim 14.0 安装教程

    1.  安装环境: win 7 64bit + Multisim 14.0 教育版. 2. 安装步骤: step 1:下载安装包NI_Circuit_Design_Suite_14.0***,解压后如 ...

  9. Competition Set - Codeforces

    这里记录的是这个账号的比赛情况. Codeforces Round 942 (Div. 1) Solved:6/8,AB1B2CDE1 2645-> A 题意:现有 \(a_i\) 张写有 \( ...

  10. JUC并发编程学习笔记(二)Lock锁(重点)

    Lock锁(重点) 传统的synchronized 传统的解决多线程并发导致的一些问题我们会使用synchronized关键字来解决,synchronized的本质就是队列.锁. Lock的实现类有: ...