原题链接在这里: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. 【Java】仿真qq尝试:聊天界面 && 响应用户输入

    需求分析: 逐步完善一个“qq仿真”程序. 参考: 1.文本框与文本区:http://www.weixueyuan.net/view/6062.html 2.java布局:http://www.cnb ...

  2. 20145219 《Java程序设计》第05周学习总结

    20145219 <Java程序设计>第05周学习总结 教材学习内容总结 try.catch 1.求平均数程序示例 import java.util.Scanner; public cla ...

  3. MapReduce:实现文档倒序排序,且字符串拼接+年+月+日

    写出MapReduce程序完成以下功能. input1: -- a -- b -- c -- d -- a -- b -- c -- c input2: -- b -- a -- b -- d -- ...

  4. PHP练习题一

    目录:1.如何使用php导入导出csv?2.php接收POST数据的方式有哪些?3.如何让json_encode()不转义斜杠?我在做服务器返回一些数据时需要返回一些地址,但是默认的json_code ...

  5. [国家集训队2011]happiness

    Description 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友 ...

  6. SpiralOrderTraverse,螺旋遍历二叉树,利用两个栈

    问题描述:s型遍历二叉树,或者反s型遍历二叉树 算法分析:层序遍历二叉树只需要一个队列,因为每一层都是从左往右遍历,而s型遍历二叉树就要用两个栈了,因为每次方向相反. public static vo ...

  7. RabbitMQ 简单了解以及使用

    RabbitMQ 开发语言:Erlang – 面向并发的编程语言. AMQP:是消息队列的一个协议. mysql 是 java 写的吗?不是 那么 java 能不能访问?可以,则通过(驱动)协议;那么 ...

  8. Python SQL相关操作

    环境 Anaconda3 Python 3.6, Window 64bit 目的 从MySQL数据库读取数据,进行数据查询.关联 代码 # -*- coding: utf-8 -*- "&q ...

  9. CentOS下安装mysql及配置使用

    最近一直使用的是CentOS,平时用的最多的数据库是Sql Server,对于mysql还停留在上学的时候,早已忘得一干二净,写这篇内容目的是,重新学习如何安装使用mysql. 一.安装mysql 操 ...

  10. Eclipse工具栏上android的机器人小图标不见了

    可以通过「Window」⇒「Customize Perspective」⇒「Tool Bar Visibility」Tab画面上选择Android SDK and AVD Manager来显示