当前位置:网站首页>678. Valid bracket string
678. Valid bracket string
2022-07-01 10:19:00 【hequnwang10】
One 、 Title Description
Given a string containing only three characters :( ,) and *, Write a function to check whether the string is a valid string . Valid strings have the following rules :
- Any left parenthesis ( There must be a corresponding closing bracket ).
- Any closing parenthesis ) There must be a corresponding left parenthesis ( .
- Left parenthesis ( Must precede the corresponding closing bracket ).
- Can be treated as a single right parenthesis ) , Or a single left parenthesis ( , Or an empty string .
- An empty string is also considered a valid string .
Example 1:
Input : "()"
Output : True
Example 2:
Input : "(*)"
Output : True
Example 3:
Input : "(*))"
Output : True
Two 、 Problem solving
Two traversal
Forward and backward traversal , Traverse from left to right , Determine the closing bracket , Traverse from right to left , Determine left parenthesis .
class Solution {
public boolean checkValidString(String s) {
// Two traversal , Forward and backward traversal , First determine whether the left bracket has a corresponding right bracket
int left = 0,cnt = 0;
int length = s.length();
char[] sch = s.toCharArray();
for(char ch : sch){
if(ch == '('){
left++;
}else if(ch == ')'){
left--;
}else{
cnt++;
}
// Judge whether the current left bracket has a corresponding right bracket
if(left < 0){
cnt--;
left++;
}
if(cnt < 0){
return false;
}
}
// Traverse from right to left
int right = 0;
cnt = 0;
for(int i = length-1;i>=0;i--){
char ch = sch[i];
if(ch == ')'){
right++;
}else if(ch == '('){
right--;
}else{
cnt++;
}
// Judge whether the current right bracket has a corresponding left bracket
if(right < 0){
cnt--;
right++;
}
if(cnt < 0){
return false;
}
}
return true;
}
}
Time complexity :O(n)
Spatial complexity :O(1)
greedy
Traversing the string from left to right , During traversal , The number of unmatched left parentheses may change as follows :
If you encounter the left bracket , Then the number of unmatched left parentheses plus 1;
If you encounter the right bracket , You need to have a left bracket and a right bracket match , Therefore, the number of unmatched left parentheses is reduced by 1;
If you encounter an asterisk , Because the asterisk can be regarded as an open bracket 、 Right parenthesis or empty string , Therefore, the number of unmatched left parentheses may be increased by 1、 reduce 1 Or unchanged .
Based on the above conclusion , You can maintain the possible minimum and maximum values of the number of unmatched left parentheses during traversal , Update the minimum and maximum values according to the traversed characters :
If you encounter the left bracket , Then add the minimum and maximum values respectively 1;
If you encounter the right bracket , Then the minimum and maximum values are reduced by 1;
If you encounter an asterisk , Then reduce the minimum value by 1, Add... To the maximum 1.
In any case , The number of unmatched left parentheses must be non negative , So when the maximum becomes negative , No left parenthesis can match the right parenthesis , return false.
When the minimum value is 0 when , The minimum value should not be reduced , To ensure that the minimum value is non negative .
At the end of traversal , All left parentheses should match the right parenthesis , So only if the minimum value is 0 when , character string s Is a valid parenthesis string .
class Solution {
public boolean checkValidString(String s) {
// Greedy Algorithm
// Define two values , Record the maximum and minimum values of the matching left parentheses
int mincnt = 0,maxcnt = 0;
int length = s.length();
char[] sch = s.toCharArray();
for(int i = 0;i<length;i++){
char ch = sch[i];
if(ch == '('){
mincnt++;
maxcnt++;
}else if(ch == ')'){
mincnt = Math.max(mincnt-1,0);
maxcnt--;
if(maxcnt < 0){
return false;
}
}else{
mincnt = Math.max(mincnt - 1,0);
maxcnt++;
}
}
return mincnt == 0;
}
}
Time complexity :O(n)
Spatial complexity :O(1)
边栏推荐
猜你喜欢
Kotlin coprocessor scheduling switch threads it's time to unravel the truth
Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录
scratch大鱼吃小鱼 电子学会图形化编程scratch等级考试二级真题和答案解析2022年6月
Sleeping second brother...
SQL server2014 failed to delete the database, with an error offset of 0x0000
Floyd repeat
数据库的增删改查问题
程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
Who's still buying three squirrels
[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul
随机推荐
BSN长话短说之十:如何保证NFT的安全
Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
How to understand JS promise
新一代云原生数据库的设计与实践
哪个券商公司炒股开户佣金低又安全又可靠
好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与!
STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
CRC 校验
About database: how to avoid deadlock in gbase 8s
C# 一行代码计算文件的MD5值 - CodePlus系列
In terms of use
Error: missing revert data in call exception
12. Gateway new generation gateway
button按钮清除边框
大佬们 有没有搞过sink分流写入clickhouse 或者其他数据库的操作。
106. 从中序与后序遍历序列构造二叉树
怎么理解JS Promise
venv: venv 的目录结构
超标量处理器设计 姚永斌 第4章 分支预测 --4.1 小节摘录
PO模式深入封装