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();