`{"id":"c8740260-b395-4e26-a9ae-54f954a8a633","name":"Basic Calculator","description":"Given a string s representing an expression, implement a basic calculator to evaluate it.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= s.length &lt;= 3 * 10^5\r\n2. s consists of digits, '+', '-', '(', ')', and ' '.\r\n3. s represents a valid expression.\r\n4. result of the expression always lies in range of -2^31 to 2^31","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<bits/stdc++.h>\nusing namespace std;\n\nint calculate(string s){\n // write your code here \n}\n\nint main(){\n string s;\n getline(cin, s);\n \n cout<<calculate(s);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int calculate(String s){\r\n return 0;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int result = calculate(read.readLine());\r\n System.out.println(result);\r\n \r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"(1+(4+5+2)-3)+(6+8)","sampleOutput":"23\r\n","questionVideo":"https://www.youtube.com/embed/HUfUzA9Ekgo","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"512a7f8d-f827-41a6-b475-556a735fb799","name":"Basic Calculator","slug":"basic-calculator","type":1}],"next":{"id":"9c3d8c7b-7448-4e09-bfe5-9236215f8950","name":"Basic Calculator Hard MCQ","type":0,"slug":"basic-calculator-hard-mcq"},"prev":{"id":"50ad6be5-13db-4975-ab13-0d048472e860","name":"Number Of Valid Subarrays","type":3,"slug":"number-of-valid-subarrays"}}}`

# Basic Calculator

Given a string s representing an expression, implement a basic calculator to evaluate it.

`{"id":"c8740260-b395-4e26-a9ae-54f954a8a633","name":"Basic Calculator","description":"Given a string s representing an expression, implement a basic calculator to evaluate it.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= s.length &lt;= 3 * 10^5\r\n2. s consists of digits, '+', '-', '(', ')', and ' '.\r\n3. s represents a valid expression.\r\n4. result of the expression always lies in range of -2^31 to 2^31","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<bits/stdc++.h>\nusing namespace std;\n\nint calculate(string s){\n // write your code here \n}\n\nint main(){\n string s;\n getline(cin, s);\n \n cout<<calculate(s);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int calculate(String s){\r\n return 0;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int result = calculate(read.readLine());\r\n System.out.println(result);\r\n \r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"(1+(4+5+2)-3)+(6+8)","sampleOutput":"23\r\n","questionVideo":"https://www.youtube.com/embed/HUfUzA9Ekgo","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"512a7f8d-f827-41a6-b475-556a735fb799","name":"Basic Calculator","slug":"basic-calculator","type":1}],"next":{"id":"9c3d8c7b-7448-4e09-bfe5-9236215f8950","name":"Basic Calculator Hard MCQ","type":0,"slug":"basic-calculator-hard-mcq"},"prev":{"id":"50ad6be5-13db-4975-ab13-0d048472e860","name":"Number Of Valid Subarrays","type":3,"slug":"number-of-valid-subarrays"}}}`

Editor

# Basic Calculator

hard

Given a string s representing an expression, implement a basic calculator to evaluate it.

## Constraints

1. 1 <= s.length <= 3 * 10^5 2. s consists of digits, '+', '-', '(', ')', and ' '. 3. s represents a valid expression. 4. result of the expression always lies in range of -2^31 to 2^31

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}(1+(4+5+2)-3)+(6+8)`

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}23 ```

Question Video

Discussions

Show Discussion

Related Resources