For example, for 20 - Valid Parentheses, my exact submission:
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
let stack = [];
for (let i = 0; i < s.length; i++) {
let char = s[i];
if (char === '(' || char === '{' || char === '[') {
stack.push(char)
} else {
if (!stack.length) return false;
let prevChar = stack.pop();
if (char === ')' && prevChar !== '(') {
return false;
}
if (char === '}' && prevChar !== '{') {
return false;
}
if (char === ']' && prevChar !== '[') {
return false;
}
}
}
return !stack.length;
};Back ~1 year ago, my submission ran at ~50ms, ~35MB each time, and I ran it a couple times.
Nowadays, the exact code takes ~80ms, ~80MB every time, and I ran it a few times.
I've tried this a few times with a few problems and observed this pattern. Not sure if anyone else has.
Thanks!