Complexity of Final Solution
Let n be the length of the input. We iterate through the input once for O(n) time complexity, and we can have up to n elements on the stack (for example having an input only containing () for O(n) space complexity as well.
Let's see it all together now.
xxxxxxxxxx23
var assert = require('assert');function calculator(str) { // implement this method return str;}try { assert.equal(calculator('2 + 2'), 4); console.log('PASSED: ' + "assert.equal(calculator('2 + 2'), 4)");} catch (err) { console.log(err);}try { assert.equal(calculator('(2+2) - 3'), 1); console.log('PASSED: ' + "assert.equal(calculator('(2+2) - 3'), 1)");} catch (err) { console.log(err);}OUTPUT
Results will appear here.