Basic Calculator Problem

Basic Calculator Problem — ExecCode Medium DSA Practice

Solve the Basic Calculator problem on ExecCode. Free online medium DSA practice in Stack. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given a string s representing a valid expression, implement a basic calculator to evaluate it. s contains digits, +, -, (, ), and spaces.

Examples

Input s = "(1+(2+3))-4"; Output 2. Input s = "2-(1+2)"; Output -1. Input s = "1+(2+3)"; Output 6

Constraints

1 ≤ s.length ≤ 3 × 10⁵ s consists of digits, "+", "-", "(", ")", and spaces. Expression is valid.

Practice Basic Calculator free on ExecCode. Browse DSA problems, topic map, and placement guides.