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

题目:

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].

题解:

Accumlate count of parenthesis and star.

When encountering *, increment starCount.

When encountering (, first make sure there is no more starCount than parCount.

If yes, these many more * could affect final decision. like ***(((. Finally, starCount >= parCount, but should return false.

Make starCount = parCount. Then increment parCount.

When encountering ), before decrementing parCount, if both parCount and starCount are 0, that means too many ), return false.

Finally, check if starCount >= parCount. These startCount happens after (, and they could be treated as ).

Time Complexity: O(s.length()).

Space: O(1).

AC Java:

 class Solution {
public boolean checkValidString(String s) {
if(s == null || s.length() == 0){
return true;
} int parCount = 0;
int starCount = 0;
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(c == '('){
if(starCount > parCount){
starCount = parCount;
} parCount++;
}else if(c == '*'){
starCount++;
}else{
if(parCount == 0){
if(starCount == 0){
return false;
} starCount--;
}else{
parCount--;
}
}
} return starCount >= parCount;
}
}

When encountering ( or ), it is simple to increment or decrement.

遇到"*". 可能分别对应"(", ")"和empty string 三种情况. 所以计数可能加一, 减一或者不变. 可以记录一个范围[lo, hi].

lo is decremented by * or ).

hi is incremented by * or (.

When hi < 0, that means there are too many ), return false.

If lo > 0 at the end, it means there are too many ( left, return false.

Since lo is decremented by * and ), it could be negative. Thus lo-- should only happen when lo > 0 and maintain lo >= 0.

Note: lo can't be smaller than 0. When hi < 0, return false.

Time Complexity: O(s.length()).

Space: O(1).

AC Java:

 class Solution {
public boolean checkValidString(String s) {
if(s == null || s.length() == 0){
return true;
} int lo = 0;
int hi = 0;
for(int i = 0; i<s.length(); i++){
if(s.charAt(i) == '('){
lo++;
hi++;
}else if(s.charAt(i) == ')'){
lo--;
hi--;
}else{
lo--;
hi++;
} if(hi < 0){
return false;
}
lo = Math.max(lo, 0);
} return lo == 0;
}
}

LeetCode Valid Parenthesis String的更多相关文章

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

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

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

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

  3. leetcode 678. Valid Parenthesis String

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

  4. 678. Valid Parenthesis String

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

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

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

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

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

  7. 【leetcode】Valid Parenthesis String

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

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

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

  9. [LeetCode] Special Binary String 特殊的二进制字符串

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

随机推荐

  1. CSS 边距和填充

    margin and padding are the two most commonly used properties for spacing-out elements. A margin is t ...

  2. 微服务与SOA

    微服务跟SOA有什么区别呢,可以把微服务当做去除了ESB的SOA.ESB是SOA架构中的中心总线,拓扑结构应该是星形的,而微服务是去中心化的分布式软件架构. 一.巨石(monolith) web应用程 ...

  3. readonly、disabled、display、visible的区别

    display和visible的区别: (1)首先说明的是display:none和visible:hidden都能够实现将网页上某个元素隐藏起来. (2)如果在样式文件或页面文件代码中直接用disp ...

  4. 防止CSRF的攻击—Origin和Referer

    防止CSRF的攻击—Origin和Referer 为了防止CSRF的攻击,我们建议修改浏览器在发送POST请求的时候加上一个Origin字段,这个Origin字段主要是用来标识出最初请求是从哪里发起的 ...

  5. Appium+eclipse+python环境配置

    1.安装安卓开发环境(教程很多,不细写) 2.安装eclipse 下载eclipse,解压即可   3.安装python 下载地址:https://www.python.org/downloads/r ...

  6. 迁移cnblog博客

    title: 迁移cnblog博客 date: 2018-03-18 categories: cnblogs tags: life 写博客最怕就是这种迁来迁去的,太麻烦了 还好老早就使用markdow ...

  7. KMP(转自matrix67)

    蒟蒻笔者自己发现matrix67大佬讲的十分明白,然后现在网络上的排版也是有些微的问题,就稍稍改了一下,然后把代码改成了c++的. 如果机房马上要关门了,或者你急着要和MM约会,请直接跳到第六个自然段 ...

  8. 将 sql 数据库 编码 改成 Chinese_PRC_CS_AS

    use master go ) drop procedure [dbo].[p_killspid] GO create proc p_killspid ) --要关闭进程的数据库名 as ) ) de ...

  9. NextPermutation,寻找下一个全排列

    问题描述:给定一个数组是一个全排列,寻找下一个全排列.例如123->132, 321->123, 115->151. 算法分析:从后往前寻找顺序,找到后从往前寻找第一个大于当前元素, ...

  10. scala学习手记27 - 下划线与参数

    在Scala里,下划线(_)可以表示函数值的参数.如果某个参数在函数里仅使用一次,就可以用下划线表示.每次在函数里用下划线,都表示随后的参数. val arr = Array(1, 2, 3, 4, ...