I'm currently trying to solve the Valid Parenthesis problem on Leetcode, in Java, that goes as follows:
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type"
I followed the hints which told me to use a stack. Here is my code:
import java.util.Stack;
class Solution {
public boolean isValid(String s) {
Stack<Character> values = new Stack<>();
for (int i=0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
values.push(s.charAt(i));
}
if ((s.charAt(i) == ')' && values.peek() == '(') || (s.charAt(i) == ']' && values.peek() == '[') || ((s.charAt(i) == '}') && values.peek() == '{')) {
values.pop();
}
}
if (values.empty()) {
return true;
} else {
return false;
}
}
}
This passes all the test cases, however when I press submit, it gives me the following error:
java.util.EmptyStackException
at line 103, java.base/java.util.Stack.peek
at line 11, Solution.isValid
at line 56, DriverSolution.helper
at line 86, Driver.main
After looking at the documentation for java.util.Stack, I've seen that under the method peek() it says:
Throws:
EmptyStackException - if this stack is empty.
I understand it's to do with using peek() when the stack is empty, but I'm confused as to how to fix it since it somehow passed the tests before that. How do I fix this in my code?
I'm currently trying to solve the Valid Parenthesis problem on Leetcode, in Java, that goes as follows:
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type"
I followed the hints which told me to use a stack. Here is my code:
import java.util.Stack;
class Solution {
public boolean isValid(String s) {
Stack<Character> values = new Stack<>();
for (int i=0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
values.push(s.charAt(i));
}
if ((s.charAt(i) == ')' && values.peek() == '(') || (s.charAt(i) == ']' && values.peek() == '[') || ((s.charAt(i) == '}') && values.peek() == '{')) {
values.pop();
}
}
if (values.empty()) {
return true;
} else {
return false;
}
}
}
This passes all the test cases, however when I press submit, it gives me the following error:
java.util.EmptyStackException
at line 103, java.base/java.util.Stack.peek
at line 11, Solution.isValid
at line 56, DriverSolution.helper
at line 86, Driver.main
After looking at the documentation for java.util.Stack, I've seen that under the method peek() it says:
Throws:
EmptyStackException - if this stack is empty.
I understand it's to do with using peek() when the stack is empty, but I'm confused as to how to fix it since it somehow passed the tests before that. How do I fix this in my code?
Share Improve this question edited Mar 31 at 16:26 pxkaa asked Mar 31 at 16:24 pxkaapxkaa 11 bronze badge 5 |2 Answers
Reset to default 0The problem arises because when a closing parenthesis appears with values empty, then your code enters the second if and tries a peek on the empty stack, what you should do is take two different paths if values is empty or if it is not.
public boolean isValid( String s ) {
Stack<Character> values = new Stack<>();
for( int i = 0; i < s.length(); i ++ ) {
if( s.charAt( i ) == '(' || s.charAt( i ) == '[' || s.charAt( i ) == '{' ) {
values.push( s.charAt( i ) );
}
if( ! values.isEmpty() ) {
if( s.charAt( i ) == ')' && values.peek() == '(' ||
s.charAt( i ) == ']' && values.peek() == '[' ||
s.charAt( i ) == '}' && values.peek() == '{' ) {
values.pop();
}
}
else {
if( s.charAt( i ) == ')' ||
s.charAt( i ) == ']' ||
s.charAt( i ) == '}' ) {
// if we have a closing parenthesis and "values" is empty, the string is not valid.
return false;
}
}
}
return true;
}
On looking into this, it seems like you are running into a test case that specifically checks for this on submission. Leetcode has some basic test cases before the actual ones they run to consider a solution Submitted
. @Marce has put the code already.
I would like to summarize that in your code you need to check values.isEmpty()
before you call peek. With respect to the problem, it is basically failed if the stack is empty before you process the closing bracket.
Pro tip: consider changing your return to return values.isEmpty();
[)]
as well... – Jon Skeet Commented Mar 31 at 16:57else
after the firstif
. – user207421 Commented Apr 2 at 6:48