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 ...
随机推荐
- 如何用纯 CSS 创作一个小球上台阶的动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/PBGJwL 可交互视频 ...
- 20145235李涛《网络对抗》逆向及Bof基础
上学期实验楼上做过这个实验 直接修改程序机器指令,改变程序执行流程 首先进行反汇编 我们所要修改的是,程序从foo返回,本来要返回到80484ba,而我们要把80484ba修改为getshell的 ...
- git 强制放弃本地修改(新增、删除文件)【转】
本文转载自:https://blog.csdn.net/u012672646/article/details/56676804 本地修改了一些文件,其中包含修改.新增.删除的,不需要了想要丢弃,于是做 ...
- iOS动画进阶 - 手摸手教你写 Slack 的 Loading 动画
如果移动端访问不佳,可以访问我的个人博客 前几天看了一篇关于动画的博客叫手摸手教你写 Slack 的 Loading 动画,看着挺炫,但是是安卓版的,寻思的着仿造着写一篇iOS版的,下面是我写这个动画 ...
- fabric文件上传打包与校验
- PHP正则表达式 /i, /is, /s, /isU等 都是些什么东西呢?
PHP正则表达式 /i, /is, /s, /isU等 都是些什么东西呢? i 不区分大小写 s 模式中的圆点元字符(.)匹配所有的字符,包括换行符 x 模式中的空白字符除了被转义的或在字符类中的以外 ...
- bzoj 3884 上帝与集合的正确用法 指数循环节
3884: 上帝与集合的正确用法 Time Limit: 5 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 根据一些 ...
- 卸载oracle11g
完全卸载oracle11g步骤:1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务.2. 开始->程序->Oracle - OraHome ...
- C++ 学习使用
1.C++已经不太建议用#define const用法 定义小函数,不改变内部成员的可以用inline 2.namespace使用 3.C语言预处理 #define这些的使用 #pragma once ...
- linux find命令使用(转)
常用命令 find (目录) [-type d | f] (文件夹 | 文件) -name (名称,可使用正则表达式) find /root -name "*core&q ...