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 ...
随机推荐
- 【笔记】IntelliJ IDEA配置Hibernate
参考:imooc:http://www.imooc.com/video/7706 1.创建Hibernate的配置文件. 将依赖包导入项目.http://blog.csdn.net/a15337525 ...
- 【Java】仿真qq尝试:聊天界面 && 响应用户输入
需求分析: 逐步完善一个“qq仿真”程序. 参考: 1.文本框与文本区:http://www.weixueyuan.net/view/6062.html 2.java布局:http://www.cnb ...
- div 文章内容自动分屏显示
<head runat="server"> <title></title> <script language="javascri ...
- ASP.NET MVC 4.0 中使用NPOI 2.2.0 按模板生成Excel报表
使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/ ...
- Centos/ubuntu配置SVN服务
Centos安装svn yum -y install subversion ubuntu安装svn apt-get install subversion Centos配置svn root@hello: ...
- [翻译]用PostCSS改善你的CSS代码质量
“代码质量”这个术语对于程序员来说并不陌生.毕竟,每个开发人员都知道,代码只是能工作是不够的.它还应该具备其他要素:它应该是可读的,良好的格式和一致性.它也应该符合一些标准的量化指标.不过这些在写CS ...
- 编写基本的 udev 规则
来自: https://linux.cn/article-9365-1.html?utm_source=index&utm_medium=more 读者对象 理解 udev 背后的基本概念,学 ...
- spring security使用数据库验证的逻辑处理
前面做了多个示例,包括使用jdbc和hibernate两种方式访问数据库获取用户信息和权限信息,其中一些关键步骤如下: 我们在SecurityConfig中配置覆盖configure方法时候,可以 ...
- JavaWeb -- Struts 数据传输:OGNL和类型转换
1. 数据传输:OGNL和类型转换 OGNL和struts2 OGNL:Object-Graph Navigation Language. OGNL是集成进struts框架中比较强大的技术有助于数据传 ...
- Divide two numbers,两数相除求商,不能用乘法,除法,取模运算
问题描述:求商,不能用乘法,除法,取模运算. 算法思路:不能用除法,那只能用减法,但是用减法,超时.可以用位移运算,每次除数左移,相当于2倍. public class DividTwoInteger ...