Skip to content

Instantly share code, notes, and snippets.

@eokoneyo
Last active September 21, 2016 14:58
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 eokoneyo/c766020d72aa89d0fd82a5f7ace7ef9c to your computer and use it in GitHub Desktop.
Save eokoneyo/c766020d72aa89d0fd82a5f7ace7ef9c to your computer and use it in GitHub Desktop.
//Codilty test for a test scenario on exmaple '(())))(' where example is split at the index K, where K
//is such that 0 <= K <= N, and N is size of K. At K the number of open
//brackets equals the number of closing brackets
function solution(S) {
// write your code in JavaScript (Node.js 6.4.0)
var index, left, right;
var len = S.length;
var filter = Array.prototype.filter;
for (var i = 0; i< len; i++) {
left = S.slice(0, i);
right = S.slice(i+1, S.size);
var leftCount = filter.call(left, (x)=>{
return x == "(";
});
var rightCount = filter.call(right, (x)=>{
return x == ")";
});
if(leftCount.length === rightCount.length) {
index = i+1;
}
}
return index;
}
var S= "(())))(";
var result = solution(S);
console.log(`index that satisfies solution is: ${result}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment