I've a simple problem,
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', and in any positions ) so that the resulting parentheses string is valid.
Formally, a parentheses string is valid if and only if: It is the empty string, or It can be written as AB (A concatenated with B), where A and B are valid strings, or It can be written as (A), where A is a valid string. Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid.
This is my solution in JS,
const minAddToMakeValid = S => {
const stack = [];
let count = 0;
S.map(c => {
if(c === '('){
stack.push(c);
}
else if(c === ')' && stack[stack.length -1] === '('){
stack.pop();
}
else{
count ++;
}
});
return count + stack.length;
};
const S = "())";
console.log(minAddToMakeValid(S));
I get the following error,
TypeError: S.map is not a function
at minAddToMakeValid (/Users/melissa/Dropbox/js/leetcode-js/bin/921_minAddToMakeParanthesisValid.js:4:7)
at Object.<anonymous> (/Users/melissa/Dropbox/js/leetcode-js/bin/921_minAddToMakeParanthesisValid.js:19:13)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
at executeUserCode (internal/bootstrap/node.js:342:17)
at startExecution (internal/bootstrap/node.js:276:5)