Created
August 31, 2021 18:13
-
-
Save 62mkv/d95dbe2ca70ca6530fe4774d7b9f4a9e to your computer and use it in GitHub Desktop.
JS solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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