import java.util.Stack;
class Valid_Parantheses {
public boolean isValid(String s) {
Stack<Character>stack = new Stack<Character>();
for(char c : s.toCharArray()){
if(c == '('){
stack.push(')');
}else if(c == '['){
stack.push(']');
}else if(c == '{'){
stack.push('}');
}else if(stack.isEmpty() || stack.pop()!= c){
return false;
}
}
return stack.isEmpty();
}
}
※ toCharArray() : The Java string toCharArray() method converts the given string into a sequence of characters. The returned array length is equal to the length of the string.
return: returns a newly allocated character array.
※isEmpty(): Java.lang.String.isEmpty() String method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false. (Null and empty is not the same)
'Leetcode' 카테고리의 다른 글
[java] 21. Merge Two Lists (0) | 2024.01.09 |
---|---|
[java] 1.TwoSum (1) | 2024.01.02 |