Description Title:
Given only comprises a '(', ')', '{', '}', '[', ']' string, determines whether the string is valid.
Valid string to be fulfilled:
the left bracket must be of the same type of closed right parenthesis.
Left parenthesis must be closed in the correct order.
Note the empty string can be considered a valid string.
practice
Use the stack to assist solved.
1. Create an empty stack;
2, using the string loop 3 traverse turn, exit the loop transfer is complete traversal 7;
3, if the current character is a '(', '{', '[' is push, turn 2;
4, if the current character is a ')', '}', ']', switch 5;
5, if the stack is empty, it returns false, the matching is not successful, the program ends; the stack is not empty, switch 6;
6, popped top elements, brackets if not the top element to the current traversal character to match, it returns false, matching is not successful, the program ends; otherwise turn 2;
7, if the stack is empty, then the match is successful, returns true, the program ends ; otherwise it returns false, matching is not successful, the program ends.
import java.util.Stack;
class Solution {
public boolean isValid(String s) {
Stack<Character> stack_match = new Stack<Character>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='(' || s.charAt(i)=='[' || s.charAt(i)=='{'){
stack_match.push(s.charAt(i));
}else if(s.charAt(i)==')' || s.charAt(i)==']' || s.charAt(i)=='}'){
if(stack_match.isEmpty())
return false;
char current = stack_match.pop();
if(current == '(' && s.charAt(i)!=')')
return false;
if(current == '[' && s.charAt(i)!=']')
return false;
if(current == '{' && s.charAt(i)!='}')
return false;
}
}
if(stack_match.isEmpty())
return true;
return false;
}
}
Remarks
Case 1: "()"
1. Create an empty stack;
2, the string traversal;
3, the first element is "(" push; current element has the stack "(";
4, the second element is ")", a non-empty stack, pop the top element; stack elements, "(" and ")" can match;
5, traverse the string is completed, the stack empty, return true
Case 2: "{} []"
1, to create an empty stack;
2, the string traversal;
3, a first element is "{" push; current stack elements are "{";
4, the second element is "}", a non-empty stack, pop the top element; stack of elements is "{" and "}" can match;
5, the third element is the "[", into the stack; current stack elements have " [ ";
6, the second element is"] ", a non-empty stack, pop the top element; stack elements" [ "and"] "match;
7, the string is completed traversed, the stack empty, return true
Case 3: "())]"
1, to create an empty stack;
2, the string traversal;
3, the first element is "(" push; current element has the stack "(";
4, the second element is ")", a non-empty stack, pop the top element; stack elements, "(" and ") match;
5, third element"), "empty stack, returns false, the matching is unsuccessful, end of program.
Case 4: "((({[}]))"
1. Create an empty stack;
2, the string traversal;
3, the first element is "(" push; current element has the stack "(";
4, the second element is "(" push; current element has the stack "((" ;;
5, third element "(" push; current element has the stack "(((";
6, the fourth element is "{" push; current element has the stack "((({";
7, the fifth element is the "[" push; current element has the stack "((({[";
8 , the sixth element is "}", a non-empty stack, pop the top element; stack elements "[" and "}" can not match, it returns false, the program ends.
Welcome attention
Sweep under the Fanger Wei code to follow: