Skip to content

Instantly share code, notes, and snippets.

@burakcan
Created March 26, 2017 12:08
Show Gist options
  • Save burakcan/3483134b5e3c5f1e7767d2b8f2bbed34 to your computer and use it in GitHub Desktop.
Save burakcan/3483134b5e3c5f1e7767d2b8f2bbed34 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/bemiyo
<!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>
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