Skip to content

Instantly share code, notes, and snippets.

/working.js Secret

Created July 23, 2017 15:53
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 anonymous/3db015403c07f56e7d0ce9053aeb18e1 to your computer and use it in GitHub Desktop.
Save anonymous/3db015403c07f56e7d0ce9053aeb18e1 to your computer and use it in GitHub Desktop.
/*
* Complete the function below.
*/
function balancedOrNot(expressions, maxReplacements) {
var arrayLength = expressions.length;
var res = Array(arrayLength).fill(1);
for(var i = 0; i < arrayLength; i++)
{
var stringLength = expressions[i].length;
var checkString = expressions[i];
var replacementCount = maxReplacements[i];
var openCount = 0;
for(var j = 0; j < stringLength; j++)
{
if (checkString[j] === '<')
{
openCount += 1;
}
if (checkString[j] === '>')
{
if(openCount > 0)
{
openCount -= 1;
}
else if (replacementCount > 0)
{
replacementCount -= 1;
}
else if (replacementCount === 0)
{
res[i] = 0;
}
}
}
if(openCount > 0)
{
res[i] = 0;
}
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment