fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. process.stdin.on('data', function (chunk) {
  5. const input = chunk.trim();
  6. if (Math.abs(input.length) <= 5000) {
  7. const stack = [];
  8. for (const char of input) {
  9. if (char === '(') stack.push(char);
  10. else if (char === ')') {
  11. if (stack.length > 0 && stack[stack.length - 1] === '(') stack.pop();
  12. else stack.push(char);
  13. }
  14. else if (char === '?') {
  15. if (stack.length > 0 && stack[stack.length - 1] === '(') stack.pop();
  16. else stack.push(char);
  17. }
  18. }
  19. console.log(stack.length);
  20. }
  21. else console.log("La cadena supera el límite de longitud permitido.");
  22. });
  23.  
Success #stdin #stdout 0.07s 35960KB
stdin
(?))
stdout
2