1
class Solution {
2
public:
3
bool validateStackSequences(vector<int> &pushed, vector<int> &popped) {
4
stack<int> st; // Create a stack
5

6
int j = 0; // Intialise one pointer pointing on popped array
7

8
for (auto val : pushed) {
9
st.push(val); // insert the values in stack
10
while (st.size() > 0 && st.top() == popped[j]) { // if st.peek() values equal to popped[j];
11
st.pop(); // then pop out
12
j++; // increment j
13
}
14
}
15
return st.size() == 0; // check if stack is empty return true else false
16
}
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0