Created
March 26, 2017 12:08
-
-
Save burakcan/3483134b5e3c5f1e7767d2b8f2bbed34 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/bemiyo
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function solution(S) { | |
let k = Math.floor(S.length / 2); | |
while (true) { | |
const opening = (S.slice(0, k).match(/\(/g) || []).length; | |
const closing = (S.slice(k, S.length).match(/\)/g) || []).length; | |
if (opening === closing) { | |
return k; | |
} else if (opening < closing) { | |
k += 1; | |
} else { | |
k -= 1; | |
} | |
} | |
} | |
console.log(solution("(()()())")); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function solution(S) { | |
let k = Math.floor(S.length / 2); | |
while (true) { | |
const opening = (S.slice(0, k).match(/\(/g) || []).length; | |
const closing = (S.slice(k, S.length).match(/\)/g) || []).length; | |
if (opening === closing) { | |
return k; | |
} else if (opening < closing) { | |
k += 1; | |
} else { | |
k -= 1; | |
} | |
} | |
} | |
console.log(solution("(()()())"));</script></body> | |
</html> |
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
function solution(S) { | |
let k = Math.floor(S.length / 2); | |
while (true) { | |
const opening = (S.slice(0, k).match(/\(/g) || []).length; | |
const closing = (S.slice(k, S.length).match(/\)/g) || []).length; | |
if (opening === closing) { | |
return k; | |
} else if (opening < closing) { | |
k += 1; | |
} else { | |
k -= 1; | |
} | |
} | |
} | |
console.log(solution("(()()())")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment