678. Valid Parenthesis String
Medium

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

Note:

  1. The string size will be in the range [1, 100].

大致思路是:自左向右遍历时, 记录当前最多能匹配多少右括号(high表示),至少还要匹配多少右括号(low表示)。因为high的数值对后面的右括号还起作用,要维护这一变量,当某一时刻low<0时。说明右括号对于左侧过多。*号能根据后面右括号的情况而变化,遍历过程中只要low>=0,就行了。当遍历到一个左括号是,标志着它能用于抵消后面的右括号,也标志着,后面必须要有右括号或*号与其抵消。

类似只有'(' 和 ')' 的情况,count分别加减1,看是否最终等于0. 这里存在*情况,所以变为[low, high]

时间复杂度O(n),空间复杂度O(1).

class Solution {
public:
bool checkValidString(string s) {
if (s.size() == ){
return true;
}
int low = , high = ; for (int i = ; i < s.length(); i++) {
if (s[i] == '(') {
low++;
high++;
} else if (s[i] == ')') {
if (low > ) {
low--;
}
high--;
} else {
if (low > ) {
low--;
}
high++;
}
if (high < ) {
return false;
}
}
return low == ;
}
};

leetcode 678. Valid Parenthesis String的更多相关文章

  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 验证括号字符串

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

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

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

  4. 678. Valid Parenthesis String

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

  5. 【leetcode】Valid Parenthesis String

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

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

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

  7. LeetCode Valid Parenthesis String

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

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

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

  9. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

随机推荐

  1. Vue(day7)

    一.环境搭建 下面我们需要为后面要做的Vue项目搭建开发环境. 1.基本的运行环境 该项目使用node& vue在webpack环境下进行开发.首先安装基本的模块文件: npm install ...

  2. Python消息队列(RabbitMQ)

    RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用.可维护多个队列,可实现消息的一对一和广播等方式发送 RabbitMQ是一个开源的AMQP实现 ...

  3. Linux 中查看进程及资源使用情况

    top 自带的 top 命令类似于平时我们使用的任务管理器,能够列出当前系统中的进程及资源的使用情况. $ man top top - display Linux tasks 使用起来很简单,不加任何 ...

  4. DotNetCore跨平台~聊聊中间件

    回到目录 在进行.net core平台之后,我们如果希望在请求过程中添加一些事件是非常容易的,你可以把这些事件做成一个中间件Middleware,然后这些中间件就会以Http pipeline的管道方 ...

  5. 接口自动化:HttpClient + TestNG + Java(四) - 封装和测试post方法请求

    在上一篇中,我们对第一个自动化接口测试用例做了初步优化和断言,这一篇我们处理POST请求. 4.1 发送POST方法请求 post方法和get方法是我们在做接口测试时,绝大部分场景下要应对的主要方法. ...

  6. JavaScript夯实基础系列(四):原型

      在JavaScript中有六种数据类型:number.string.boolean.null.undefined以及对象,ES6加入了一种新的数据类型symbol.其中对象称为引用类型,其他数据类 ...

  7. WebApiClient的SteeltoeOSS.Discovery扩展

    1 背景 从园子里看到一些朋友在某些项目开发中,选择的架构是spring cloud搭建底层微服务框架,dotnet core来编写业务逻辑,SteeltoeOSS.Discovery是dotnet和 ...

  8. Mysql、SqlServer、Oracle三大数据库的区别

    一.MySQL 优点: 体积小.速度快.总体拥有成本低,开源: 支持多种操作系统: 是开源数据库,提供的接口支持多种语言连接操作 : MySQL的核心程序采用完全的多线程编程.线程是轻量级的进程,它可 ...

  9. 通过 JSONP 实现跨域请求

    优质参考资料:https://www.cnblogs.com/chiangchou/p/jsonp.html https://blog.csdn.net/hansexploration/article ...

  10. 数据库管理工具DataGrip使用总结(一)

    DataGrip是JetBrains公司推出的管理数据库的产品,对于JetBrains公司,开发者肯定都不陌生,IDEA和ReSharper都是这个公司的产品,用户体验非常不错. 下载地址:https ...