Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Runtime Error
Exception in thread main java.util.EmptyStackException at java.util.Stack.peek(Stack.java:102) at Solution.validStackPermutation(Solution.java:16) at Runner.executeAndPrintOutput(Runner.java:61) at Runner.main(Runner.java:73)


What I have tried:

 while (!s.empty() && s.peek() == other.get(j)) {
                s.pop();
                j++;
            }
 
            if (j <=other.size() && s.peek() == other.get(j)) {
                return false;
            }
        }
 
        return true;
        
    }
}
Posted
Updated 28-Jun-23 10:05am
v2
Comments
Dave Kreskowiak 28-Jun-23 14:24pm    
Edit this post and ADD THE ERROR MESSAGE. Your also probably going to be asked for the rest of the code too.

1 solution

Java
            while (!s.empty() && s.peek() == other.get(j)) { // if condition is never met, you quit loop when s is empty
                s.pop();
                j++;
            }
 
            if (j <=other.size() && s.peek() == other.get(j)) { // how do you know that s is not empty at this point ?
                return false;
            }
        }
 
        return true;
        
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900