# Word Machine SOLUTIONS Weekly Contest 25

## Word Machine Solution

You are given elite of strings operations where every component is either:

A non-negative number that ought to be driven into a stack

“POP” which means pop the top component in the stack

“DUP” which means copy the top component in the stack

“+” which means pop the best two and push the whole

“- ” which means pop the best two and push top – second

Return the top component in the stack subsequent to applying all activities. In the event that there are any invalid tasks, return – 1.

Requirements

n ≤ 100,000 where n is the length of operations

Model 1

Information

operations = [“1”, “5”, “DUP”, “3”, “- “]

Yield

– 2

Clarification

Following the activities:

We drive 1 into the stack: [1]

We drive 5 into the stack: [1, 5]

We copy the top component: [1, 5, 5]

We drive 3 into the stack: [1, 5, 5, 3]

We pop 3 and 5 and push their distinction 3 – 5: [1, 5, – 2]

We return the top component which is – 2

Model 2

Information

operations = [“+”]

Yield

– 1

Clarification

There’s no components in the stack so this is invalid.

Related