One minute
Leetcode 946
The idea of this solution is to append the pushed value to the stack. If peeked value is equal to the poped value then pop it off the stack, keep poping off the peeked value if it is equal to the poped value. This can be show in an image
func validateStackSequences(pushed []int, popped []int) bool {
stack := []int{}
popCounter := 0
for i := 0; i < len(pushed); i++ {
stack = append(stack, pushed[i])
for len(stack) != 0 && popped[popCounter] == stack[len(stack)-1] {
stack = stack[:len(stack)-1]
popCounter++
if popCounter == len(popped) {
break
}
}
}
return len(stack) == 0
}
Read other posts