LeetCode Valid Parenthesis String
原题链接在这里: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:
- Any left parenthesis
'('must have a corresponding right parenthesis')'. - Any right parenthesis
')'must have a corresponding left parenthesis'('. - Left parenthesis
'('must go before the corresponding right parenthesis')'. '*'could be treated as a single right parenthesis')'or a single left parenthesis'('or an empty string.- An empty string is also valid.
Example 1:
Input: "()"
Output: True
Example 2:
Input: "(*)"
Output: True
Example 3:
Input: "(*))"
Output: True
Note:
- 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的更多相关文章
- [LeetCode] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- leetcode 678. Valid Parenthesis String
678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...
- 678. Valid Parenthesis String
https://leetcode.com/problems/valid-parenthesis-string/description/ 这个题的难点在增加了*,*可能是(也可能是).是(的前提是:右边 ...
- [leetcode]678. Valid Parenthesis String验证有效括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode] 678. Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- 【leetcode】Valid Parenthesis String
题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...
- [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
随机推荐
- Nginx 启动脚本
Nginx 启动脚本 1.vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # S ...
- ubuntu 安装pip
apt-get install python3-pip
- 20145201《Java程序设计》第8周学习总结
20145201 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章 通用API 15.1 日志 15.1.1 日志API简介 java.util.logging包提供了日志功 ...
- javascript设计模式 - 解释器模式(interpreter)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [Android]热修复框架AndFix测试说明
AndFix,全称是Android hot-fix.是阿里开源的一个热补丁框架,允许APP在不重新发布版本的情况下修复线上的bug.支持Android 2.3 到 6.0,并且支持arm 与 X86系 ...
- 尽可能的构建一个拓展性比"较好"的项目,会让你后期迭代好受点
转载请注明出处:王亟亟的大牛之路 这礼拜基本都在忙自己项目上的事,然后之后会"重新整理"后把这部分的功能开源出来,这里@下队友 NeglectedByBoss 本周还是没有停更收纳 ...
- 重装window 7系统,从做一个u盘启动盘,到装系统,很不错
老毛桃U盘启动盘制作工具是现在最流行的U盘装系统和维护电脑的专用工具,一是制作简单,几乎100%支持所有U盘一键制作为启动盘,不必顾虑以前量产U盘考虑专用工具的问题.二是制作后工具功能强大,支持GHO ...
- C# 打开电子邮件软件
使用客户端打开指定的URL 使用Process.Start方法可以在浏览器打开指定的URL.代码如下所示. [C#] //使用客户端打开“http://www.baidu.com” System.Di ...
- docker定义数据卷及数据卷的备份恢复
前言:生产环境中使用docker时,往往需要对数据进行持久化(只有把容器导出为镜像,才能够保存写的数据,否则容器删除或者停止,所有数据都会没有),或者需要在多个容器之间进行数据共享,这必然涉及容器的数 ...
- POI使用总结
一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.二. HSSF概况 HSSF 是H ...