Last active
September 21, 2016 14:58
-
-
Save eokoneyo/c766020d72aa89d0fd82a5f7ace7ef9c to your computer and use it in GitHub Desktop.
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
//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