3

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)
Sagar P. Ghagare
  • 542
  • 2
  • 12
  • 25
Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88

3 Answers3

4

A String is not an Array : it doesn't have the same methods.

However, you can use S.split("") to get an array and then map over it

Vivick
  • 3,434
  • 2
  • 12
  • 25
  • 1
    So a string in non interable in javascript? – Melissa Stewart Jan 13 '19 at 19:01
  • 2
    That's not what I said : I said it wasn't an `Array` and therefore it doesn't have access to its methods (such as `map`, `filter`, etc...) – Vivick Jan 13 '19 at 19:03
  • Documentation: [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String). – Andy Jan 13 '19 at 19:05
  • 1
    Strings are iterable https://stackoverflow.com/questions/1966476/how-can-i-process-each-letter-of-text-using-javascript. They just don’t have a map method. – evolutionxbox Jan 14 '19 at 19:33
3

S is a string, not an array. That's why you won't ba able to map it.

[...S].map() should work.

The ... is a spread operator. It will take something iterable (like a string or array) will spread it out as arguments. By placing it within array brackets, it will create a new array filled with whatever you 'spreaded' in to it.

const s = 'test';
const arr = [...s]; // = ['t', 'e', 's', 't']

const S = 'demo string';

[...S].map((char)=>{
  console.log(char);
})
JasperZelf
  • 2,731
  • 1
  • 22
  • 34
-1

public int minAddToMakeValid(String S) {

int output = 0;
int open = 0;
char[] sArr = S.toCharArray();
for(char i=0; i < sArr.length; i++){
    if(sArr[i] == '('){
        output++;
        open++;
        if((i+1 < sArr.length) && sArr[i+1] == ')'){
            output--;
            open--;
        }
        else if((i+1 < sArr.length) && sArr[i+1] == '('){
            output++;
            open++;
        }
        i = (char)(((i-'0')+1) + '0');
    }
    else if(sArr[i] == ')'){
        if(open < 1){
            output++;
        }else{
            output--;
            open--;
        }
        if((i+1 < sArr.length) && sArr[i+1] == ')' && open < 1){
            output++;
            i = (char)(((i-'0')+1) + '0');
        }
        else if((i+1 < sArr.length) && sArr[i+1] == ')' && open > 0){
            output--;
            open--;
            i = (char)(((i-'0')+1) + '0');
        }
        else if((i+1 < sArr.length) && sArr[i+1] == '('){
            output++;
            open++;
            i = (char)(((i-'0')+1) + '0');
        }
    }
}
return Math.abs(output);

}

  • The method name does not look like javascript and make sure the function is inside the block code. – darmis Oct 03 '19 at 16:48