Skip to content

Latest commit

 

History

History
39 lines (33 loc) · 852 Bytes

File metadata and controls

39 lines (33 loc) · 852 Bytes

Validate Stack Sequences

Solution 1

Stack

/**
 * Question   : 946. Validate Stack Sequences
 * Complexity : Time: O(n) ; Space: O(n)
 * Topics     : Stack
 */
class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (pushed == null && popped == null) {
            return true;
        }
        if (pushed == null || popped == null) {
            return false;
        }
        if (pushed.length != pushed.length) {
            return false;
        }

        Stack<Integer> stack = new Stack<>();
        int j = 0;

        for (int i = 0; i < pushed.length; i++) {
            stack.push(pushed[i]);
            while (!stack.isEmpty() && stack.peek() == popped[j]) {
                stack.pop();
                j++;
            }
        }

        return stack.isEmpty();
    }
}