Leetcode 150. Evaluate Reverse Polish Notation
Tags: java, algorithms, computerscience, tutorial
Published: 2021-05-26 09:56:22

# Problem Statement

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, and /. Each operand may be an integer or another expression.

Note that division between two integers should truncate toward zero.

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.

> Reverse Polish notation (RPN), also known as Polish postfix notation or simply postfix notation, is a mathematical notation in which operators follow their operands, in contrast to Polish notation (PN), in which operators precede their operands.

# Sample Test Cases

Example 1:

Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:

Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:

Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

# Constraints

* 1 <= tokens.length <= 104
* tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].


Approach : 

The approach is quite straight forward by using stack.
* Here whenever we see a digit, we simply put it inside our stack.

* When we see a `+` operator, we simply pop out top 2 elements, and just add them and put it back into the stack.

* When we see a `*` operator, we simply pop out top 2 elements, and just multiply them and put it back into the stack.

* When we see a `-` operator, we need to pop out top 2 elements, but suppose `b = stack.pop() - the first element we pop` and `a = stack.pop() - the second element we pop` and now we divide `a/b` and push it back to our stack.

* When we see a `/` operator, we need to pop out top 2 elements, but suppose `b = stack.pop() - the first element we pop` and `a = stack.pop() - the second element we pop` and now we subtract `a-b` and push it back to our stack.

# Code

```java
class Solution {
    public int evalRPN(String[] tokens) {
        if (tokens.length == 0 || tokens == null)
            return -1;
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (token.equals("+")) {
                stack.push(stack.pop() + stack.pop());
            }
            else if (token.equals("*")) {
                stack.push(stack.pop() * stack.pop());
            }
            else if (token.equals("-")) {
                int b = stack.pop();
                int a = stack.pop();
                stack.push(a - b);
            }
            else if (token.equals("/")) {
                int b = stack.pop();
                int a = stack.pop();
                stack.push(a / b);
            }
            else {
                stack.push(Integer.valueOf(token));
            }
        }
        return stack.pop();
    }
}
```

# Time Complexity and Space Complexity: 

O(lengthOfInputArray) time and O(lengthOfInputArray) space.

# Code walkthrough with test cases


Input = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Answer = 22


![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdp0t6ruvus2wzhw1ymz.png)

![4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bg6k0mfp0z1zhadsppy2.png)
 

# Github Repository:

{% github Rohithv07/LeetCodeTopInterviewQuestions %}

{% github Rohithv07/LeetCode %}