Skip to content

Instantly share code, notes, and snippets.

@alsotang
Created March 27, 2019 15:22
Show Gist options
  • Save alsotang/e3ec1f8551daa1ff211f50ea325a0e66 to your computer and use it in GitHub Desktop.
Save alsotang/e3ec1f8551daa1ff211f50ea325a0e66 to your computer and use it in GitHub Desktop.
const map = {
"(": 3n,
"[": 5n,
"{": 7n,
")": -3n,
"]": -5n,
"}": -7n
};
var isValid = function(s) {
let sum = 0n;
for (var i = 0; i < s.length; i++) {
const char = s[i];
const v = map[char];
if (v > 0) {
sum = (sum + v) * v;
} else {
const newSum = sum / -v + v;
if ((newSum - v) * -v !== sum) {
return false;
}
sum = newSum
}
}
return sum == 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment