Skip to content

Instantly share code, notes, and snippets.

@62mkv
Created August 31, 2021 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 62mkv/d95dbe2ca70ca6530fe4774d7b9f4a9e to your computer and use it in GitHub Desktop.
Save 62mkv/d95dbe2ca70ca6530fe4774d7b9f4a9e to your computer and use it in GitHub Desktop.
JS solutions
/**
Can be tested quickly with Javasript REPL extension for VS Code
*//
function test_string(str) {
var par_count = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] == '(') par_count++;
else if (str[i] == ')') par_count--;
if (par_count < 0) {
console.log(`Unbalanced at char ${par_count}, unexpected closing ")"`);
return false;
}
}
if (par_count > 0) {
console.log('Not closed "("');
return false;
}
return true;
}
test_string('((('); //= false
test_string(''); //= true
test_string("((())))))))");//= false
test_string('(())');//= true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment