[One question per day] Day8 effective parentheses

This question comes from Lituo, the link is as follows: [Programming question] Effective parentheses

1. Topic requirements

Topic display:

Given a string s that only includes '(', ')''{', '}', '[', to determine whether the string is valid.']'

A valid string must satisfy:

An opening parenthesis must be closed with a closing parenthesis of the same type.
Opening parentheses must be closed in the correct order.


Enter a description:

A string of strings, which only include '(', ')', '{', '}', '[', ']'.

Output description:

true or false

Example 1:

input: output:s = "()"
true

Example 2:

input: output:s = "()[]{}"
true

Example 3:

input: output:s = "(]"
false

Example 4:

input: output:s = "([)]"
false

Hint:
1 <= s.length <= 104
s consists only of brackets '()[]{}'


2. Problem-solving ideas

[Problem analysis]:

This topic is a use of the stack. For this topic with many situations, we can analyze the various possibilities first, and then write the code in turn. This question is about the left and right brackets. We can put the left brackets into the stack, and then compare the right brackets with the top of the stack. Remove the possibility of false, which is true.

[Problem solving ideas]:

Impossible cases:

So we can start writing, the first is to judge whether it is a left bracket, if it is, it will be pushed to the stack, if it is not, it will be a right bracket.

When we encounter closing brackets, the first possibility is to judge whether the stack is empty, because there may be too many closing brackets. After the right pair is touched, the stack is empty and there are left closing brackets. This is the case.

Then the second possibility is the case of matching or not matching. We need to take a left parenthesis from the stack, and then match it with the right parenthesis. If it matches, one will be popped out of the stack.

Finally, there are too many left brackets. When we get all the elements, if there are still elements on the stack, there are too many left brackets.


3. Reference code

class Solution {
    
    
    public boolean isValid(String s) {
    
    
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
    
    
            char ch = s.charAt(i);
            if (ch == '(' || ch == '[' || ch == '{') {
    
    
                stack.push(ch);
            } else {
    
    
                //1.右括号多
                if (stack.empty()) {
    
    
                    return false;
                }

                //2.匹配&不匹配
                char top = stack.peek();//拿到左括号
                if (top == '(' && ch == ')' || top == '[' && ch == ']' || top == '{' && ch == '}') {
    
    
                    stack.pop();//匹配
                } else {
    
    
                    return false;//不匹配
                }
            }
        }
                //3.左括号多
            if(!stack.empty()){
    
    
                return false;
            }
        
        return true;//能走到这就说明前面的错误都没犯
    }
}

Guess you like

Origin blog.csdn.net/weixin_54225715/article/details/124649038