题目:

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. 阿里云荣获可信云容器安全能力先进级认证, ACK/ACR为企业级安全护航

    阿里云关注企业级用户的Kubernetes生产落地痛点,结合企业生产环境的大量实践,全面帮助企业真正落地云原生架构.安全侧问题,是众多大中型或金融领域企业的核心关注点. 端到端云原生安全架构 早在20 ...

  2. MAE 自监督算法介绍和基于 EasyCV 的复现

    ​简介:自监督学习(Self-Supervised Learning)能利用大量无标注的数据进行表征学习,然后在特定下游任务上对参数进行微调.通过这样的方式,能够在较少有标注数据上取得优于有监督学习方 ...

  3. [FAQ] 快速准备 windows 的 nodejs 开发环境

      下载 git 版本控制系统:https://pc.qq.com/search.html#!keyword=git 下载 TortoiseGit 客户端:https://pc.qq.com/sear ...

  4. [CosmWasm] 安装 Rust 和 wasm32 (Linux & Mac)

      先用 rustup 安装 Rust 语言,再确保你拥有wasm32目标. $ rustup default stable $ cargo version # If this is lower th ...

  5. [Contract] 测试 Solidity 合约代码的两种方式 与 优缺点

    第一种,使用 Truffle 这类继承了测试工具的框架,只要编写 js 脚本就可以测试 web3 与合约的逻辑. 优点是完全可控,粒度够细,便于集成测试:缺点是需要花费一些时间编写测试脚本,不过值得. ...

  6. 使用Binlog日志恢复误删的MySQL数据实战

    前言 "删库跑路"是程序员经常谈起的话题,今天,我就要教大家如何删!库!跑!路! 开个玩笑,今天文章的主题是如何使用Mysql内置的Binlog日志对误删的数据进行恢复,读完本文, ...

  7. virutalenvwrapper安装和使用

    目录 virutalenvwrapper安装和使用 目的: 1.安装pip 2.安装virutalenv和virutalenvwrapper 3.配置环境变量 4. 创建虚拟环境 5.列出全部的虚拟环 ...

  8. kali 忘记账户密码

    kali 忘记账户密码 重启 kali 虚拟机,在开始界面不需要选择,按 e 键 找到 Linux 开头的行,将 ro 处及该行后面的字符替换为:rw init=/bin/bash 按 F10 进入命 ...

  9. Golang csv操作

    目录 csv读写 追加写入 追加写入封装 csv读写 封装成工具包 package utils import ( "encoding/csv" "fmt" &q ...

  10. Critical Expression

    什么是Critical Expression 所谓Critical Expression就是一个表达式依赖的值,必须出现在这个表达式前面.比如: times (label-$) db 0 ;times ...