Given a string s which represents an expression, evaluate this expression and return its value.

The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of [-231, 231 - 1].

You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval(). Input: s = "3+2*2"
Output: 7
Input: s = "3/2"
Output: 1
Input: s = "3+5 / 2"
Output: 5
Constraints:

Contents

In this approach, we will use a Stack to store operands and when we see a operator +, -, *, or / apply the operation on the operands.

Implementation steps:
Example:

Let's take an example s = "10-2*3+5/2"

import java.util.Stack; public class BasicCalculatorII { static int calculate(String s) { Stack<Integer> stack = new Stack<>(); int number = 0; char sign = '+'; // reason we are using this is say if the input is "10-2*3+5/2" // assume + is default sign for 10, if the starting is -10 for example, then it will 0+ at the beginning. for(int i=0; i<s.length(); i++) { char current = s.charAt(i); if(Character.isDigit(current)) { // If the character is between 0-9 number = number *10 + (current - '0'); // number *10 is to concatenate the number, // say if the input is two digit letter, // for example, 79 first we take 7, then when we get 9 we have to move 7 to 10th position, // so we multiply with 10, then add current number. } if( (!Character.isDigit(current) && current != ' ') || i == s.length() -1 ) { if(sign == '+') { stack.push(number); } else if(sign == '-') { stack.push(-number); } else if (sign == '*') { stack.push(stack.pop() * number); } else if (sign == '/') { stack.push(stack.pop() / number); } sign = current; number = 0; } } int result = 0; while(!stack.isEmpty()) { result+=stack.pop(); } return result; } public static void main(String[] args) { System.out.println(calculate("3+5 / 2 ")); System.out.println(calculate("10-2*3+5/2")); System.out.println(calculate("-10-2*3+5/2")); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of input string s.
Space complexity: O(n) for the Stack.

Above implementations source code can be found at GitHub link for Java code