Question
Given a string containing just the characters '(', ')', '{', '}', '[' and ']' , determine if the input string is valid.
Example
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
Solution
Java
public class Solution {
/**
* @param s A string
* @return whether the string is a valid parentheses
*/
public boolean isValidParentheses(String s) {
// Write your code here
if (s == null || s.length() < 2) {
return false;
}
Stack<Character> pths = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
char character = s.charAt(i);
switch (character) {
case '(':
case '{':
case '[':
pths.push(character);
break;
case ')':
if (!pths.empty() && pths.pop().equals('(')) {
break;
}
return false;
case '}':
if (!pths.empty() && pths.pop().equals('{')) {
break;
}
return false;
case ']':
if (!pths.empty() && pths.pop().equals('[')) {
break;
}
return false;
default:
return false;
}
}
return pths.empty();
}
}