TypeError : map is not a function

Viewed 8272

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)
3 Answers

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

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);
})

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);

}

Related