当前位置:网站首页>LeetCode_ 20 (brackets match)

LeetCode_ 20 (brackets match)

2022-06-10 14:25:00 ***

Title Description : Given one only includes ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ String s , Determines whether the string is valid .

Valid string needs to meet :
1、 Opening parentheses must be closed with closing parentheses of the same type .
2、 The left parenthesis must be closed in the correct order .

Example 1:
Input :s = “()”
Output :true

Example 2:
Input :s = “()[]{}”
Output :true

Example 3:
Input :s = “(]”
Output :false

Example 4:
Input :s = “([)]”
Output :false

Example 5:
Input :s = “{[]}”
Output :true

Tips :

1 <= s.length <= 104
s Brackets only ‘()[]{}’ form

class Solution {
    
    public boolean isValid(String s) {
    
        int length=s.length()/2;
        for(int i=0;i<length;i++){
    
        	// Will encounter () or 【】 or {
    } Eliminate it all like music 
            s=s.replace("()","").replace("[]","").replace("{}","");
        }
        return s.length()==0;
    }
}
原网站

版权声明
本文为[***]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101415407390.html