题目:

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. Python 潮流周刊#47:当你的老师希望你去做开源

    本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...

  2. 链栈的实现 C语言/C++

    堆栈的链式存储C/C++实现--链栈 与顺序栈相比,链栈的优点在于不存在栈满上溢的问题.链栈通常使用单链表实现,进栈.出栈操作就是在单链表表头的 插入.删除操作.用单链表实现链栈时,使用不带头结点的单 ...

  3. ECharts海量数据渲染解决卡顿的4种方式

    场景 周五进行需求评审的时候: 出现了一个图表,本身一个图表本没有什么稀奇的: 可是产品经理在图表的上的备注,让我觉得这个事情并不简单: 那个图表的时间跨度可以是月,年,而且时间间隔很短: 这让我意识 ...

  4. 迁移 Nacos 和 ZooKeeper,有了新工具

    简介: 注册中心迁移在行业中主要有两个方案,一个是双注册双订阅模式(类似数据库双写),一个是 Sync 模式(类似于数据库 DTS):MSE 同时支持了两种模式,对于开通 MSE 服务治理客户,MSE ...

  5. 001_Cadence软件的安装与介绍

    001_Cadence软件的安装与介绍 软件版本16.6,软件下载:搜索PCB联盟; 安装步骤: 1)   把5个分卷的压缩包解压到同一文件夹; 2)   双击Setup.exe开始安装,先安装Lic ...

  6. 如何用python运用ocr技术来识别文字

    要先安装ocr技术,也就是光学符号识别,通过扫描等光学输入方式将各种票据.报刊.书籍.文稿及其他印刷品的文字转化为图像信息,再利用文字识别技术将图像信息转化为可以使用的文本的技术(我在百度百科抄的), ...

  7. OpenCompass-书生浦语大模型实战营第二期第7节作业

    书生浦语大模型实战营第二期第7节作业 这一节的作业和第6节作业一样没有特别多好说的,以运行结果为主. 基础作业 使用 OpenCompass 评测 internlm2-chat-1_8b 模型在 C- ...

  8. sql 查找是否存在的记录

    场景:根据条件从数据库表中查询 『有』与『没有』,只有两种状态 方法1: SELECT count(*) FROM table WHERE a = 1 方法2: SELECT 1 FROM table ...

  9. three.js教程4-Group层级模型

    1.组对象Group.层级模型-形成树状结构 //创建两个网格模型mesh1.mesh2 const geometry = new THREE.BoxGeometry(20, 20, 20); con ...

  10. html布局浅谈

    现在布局方式主要分为三种 传统css布局方案(position,float,line-height等配合).实现复杂,需要多种属性配合使用,兼容性最好. flex布局方案.弹性布局,实现方便,兼容性较 ...