split string brackets count
by Venkata Sai Vamsi Penupothu
JavaScript
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
var firstPart = "";
var secondPart = "";
var splitPoint = 0;
var length = 0;
function solution(S) {
// write your code in JavaScript (Node.js 6.4.0)
length = S.length;
splitPoint = Math.floor(length/2);
updateParts();
//console.log(firstPart);
//console.log(secondPart);
findK();
//console.log(firstPart);
//console.log(secondPart);
//console.log(splitPoint+1);
return (splitPoint);
}
function findK(){
var l1,l2;
var isMatch = false;
while(!isMatch){
l1 = firstPart.split("(").length-1;
l2 = secondPart.split(")").length-1;
if(l1 === l2){
isMatch = true;
}else{
if(l1 >l2){
splitPoint--;
}else{
splitPoint++;
}
updateParts();
}
}
}
function updateParts(){
firstPart = S.substr(0,splitPoint);
secondPart = S.substr(splitPoint, length-1);
}