Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Time Complexity: O(N)
- *
- * Space Complexity: O(N)
- *
- * N = length of the pushed or popped array.
- */
- class Solution {
- public boolean validateStackSequences(int[] pushed, int[] popped) throws IllegalArgumentException {
- if (pushed == null || popped == null) {
- throw new IllegalArgumentException("Input array is null");
- }
- if (pushed.length != popped.length) {
- return false;
- }
- Stack<Integer> stack = new Stack();
- int i = 0;
- for (int p : pushed) {
- stack.push(p);
- while (!stack.isEmpty() && stack.peek() == popped[i]) {
- stack.pop();
- i++;
- }
- }
- return stack.isEmpty();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement