Advertisement
amancse1

Untitled

Dec 28th, 2022
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @param {number[]} pushed
  3.  * @param {number[]} popped
  4.  * @return {boolean}
  5.  */
  6. var validateStackSequences = function(pushed, popped) {
  7.    
  8.  
  9.     let arr = [];
  10.     let j = 0;
  11.  
  12.     for(let i = 0; i < pushed.length; i++) {
  13.         arr.push(pushed[i])
  14.  
  15.         if(arr[arr.length - 1] === popped[j]) {
  16.  
  17.             while(arr.length && arr[arr.length - 1] === popped[j]) {
  18.                 arr.pop();
  19.                 j++;
  20.             }
  21.         }
  22.        
  23.     }
  24.  
  25.     return arr.length === 0
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement