--DAY 6--
Hi, I am going to make #100DaysOfCode Challenge.Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:
-Problem: Valid Parentheses
-Detail: https://leetcode.com/problems/valid-parentheses/
-My solution (javascript):
var isValid = function(s) {
if(s.length%2!=0) return false;
let map = new Map([
['[',']'],['(',')'],['{','}']
])
let stack=[],closeChar=[];
for(let x of s){
if('([{'.includes(x)) stack.push(x);
else if(x == map.get(stack[stack.length-1])){
stack.pop();
}
else closeChar.push(x);
}
return(stack.length==0&&closeChar.length==0)?true:false;
};
-->If you have better solution or any question, please comment below. I will appreciate.
Top comments (4)
Are you use regex? It's cool !
Yes regex.
You could shorten it to:
many thanks