Created
February 15, 2019 05:45
-
-
Save m-alikhizar/5e5f4b2e7ed53bd128d5d19576926173 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/golojih
This file contains hidden or 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"> | |
'use strict'; | |
var getBalancedLength = function getBalancedLength(str) { | |
if (typeof str !== 'string') { | |
console.error('Invalid type string.'); | |
} | |
var token_queue = []; | |
var res = []; | |
var count = 0; | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = str[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
char = _step.value; | |
if (/\(/.test(char)) token_queue.push(1);else if (/\)/.test(char)) { | |
if (token_queue.length) { | |
token_queue.shift(); | |
count += 2; | |
} else { | |
res.push(count); | |
count = 0; | |
} | |
} else { | |
console.error('Invalid type string.'); | |
break; | |
} | |
} | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator['return']) { | |
_iterator['return'](); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
res.push(count); | |
return Math.max.apply(Math, res); | |
}; | |
console.clear(); | |
console.log(getBalancedLength('')); | |
console.log(getBalancedLength('(a)')); | |
console.log(getBalancedLength(')(')); | |
console.log(getBalancedLength('()')); | |
console.log(getBalancedLength('))(()()))')); | |
console.log(getBalancedLength('()())()((()))')); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const getBalancedLength = (str) => { | |
if(typeof str !== 'string') { | |
console.error('Invalid type string.'); | |
} | |
const token_queue = []; | |
const res = []; | |
let count = 0; | |
for(char of str) { | |
if(/\(/.test(char)) | |
token_queue.push(1); | |
else if(/\)/.test(char)) { | |
if(token_queue.length) { | |
token_queue.shift(); | |
count += 2; | |
} else { | |
res.push(count); | |
count = 0; | |
} | |
} else { | |
console.error('Invalid type string.'); | |
break; | |
} | |
} | |
res.push(count); | |
return Math.max(...res); | |
} | |
console.clear(); | |
console.log(getBalancedLength('')); | |
console.log(getBalancedLength('(a)')); | |
console.log(getBalancedLength(')(')); | |
console.log(getBalancedLength('()')); | |
console.log(getBalancedLength('))(()()))')); | |
console.log(getBalancedLength('()())()((()))')); | |
</script></body> | |
</html> |
This file contains hidden or 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
'use strict'; | |
var getBalancedLength = function getBalancedLength(str) { | |
if (typeof str !== 'string') { | |
console.error('Invalid type string.'); | |
} | |
var token_queue = []; | |
var res = []; | |
var count = 0; | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = str[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
char = _step.value; | |
if (/\(/.test(char)) token_queue.push(1);else if (/\)/.test(char)) { | |
if (token_queue.length) { | |
token_queue.shift(); | |
count += 2; | |
} else { | |
res.push(count); | |
count = 0; | |
} | |
} else { | |
console.error('Invalid type string.'); | |
break; | |
} | |
} | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator['return']) { | |
_iterator['return'](); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
res.push(count); | |
return Math.max.apply(Math, res); | |
}; | |
console.clear(); | |
console.log(getBalancedLength('')); | |
console.log(getBalancedLength('(a)')); | |
console.log(getBalancedLength(')(')); | |
console.log(getBalancedLength('()')); | |
console.log(getBalancedLength('))(()()))')); | |
console.log(getBalancedLength('()())()((()))')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment