Guest User

Untitled

a guest
Feb 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. 'use strict';
  12.  
  13. var getBalancedLength = function getBalancedLength(str) {
  14. if (typeof str !== 'string') {
  15. console.error('Invalid type string.');
  16. }
  17.  
  18. var token_queue = [];
  19. var res = [];
  20.  
  21. var count = 0;
  22.  
  23. var _iteratorNormalCompletion = true;
  24. var _didIteratorError = false;
  25. var _iteratorError = undefined;
  26.  
  27. try {
  28. for (var _iterator = str[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  29. char = _step.value;
  30.  
  31. if (/\(/.test(char)) token_queue.push(1);else if (/\)/.test(char)) {
  32. if (token_queue.length) {
  33. token_queue.shift();
  34. count += 2;
  35. } else {
  36. res.push(count);
  37. count = 0;
  38. }
  39. } else {
  40. console.error('Invalid type string.');
  41. break;
  42. }
  43. }
  44. } catch (err) {
  45. _didIteratorError = true;
  46. _iteratorError = err;
  47. } finally {
  48. try {
  49. if (!_iteratorNormalCompletion && _iterator['return']) {
  50. _iterator['return']();
  51. }
  52. } finally {
  53. if (_didIteratorError) {
  54. throw _iteratorError;
  55. }
  56. }
  57. }
  58.  
  59. res.push(count);
  60.  
  61. return Math.max.apply(Math, res);
  62. };
  63.  
  64. console.clear();
  65.  
  66. console.log(getBalancedLength(''));
  67. console.log(getBalancedLength('(a)'));
  68. console.log(getBalancedLength(')('));
  69. console.log(getBalancedLength('()'));
  70. console.log(getBalancedLength('))(()()))'));
  71. console.log(getBalancedLength('()())()((()))'));
  72. </script>
  73.  
  74.  
  75.  
  76. <script id="jsbin-source-javascript" type="text/javascript">const getBalancedLength = (str) => {
  77. if(typeof str !== 'string') {
  78. console.error('Invalid type string.');
  79. }
  80.  
  81. const token_queue = [];
  82. const res = [];
  83.  
  84. let count = 0;
  85.  
  86. for(char of str) {
  87.  
  88. if(/\(/.test(char))
  89. token_queue.push(1);
  90.  
  91. else if(/\)/.test(char)) {
  92. if(token_queue.length) {
  93. token_queue.shift();
  94. count += 2;
  95. } else {
  96. res.push(count);
  97. count = 0;
  98. }
  99. } else {
  100. console.error('Invalid type string.');
  101. break;
  102. }
  103. }
  104.  
  105. res.push(count);
  106.  
  107. return Math.max(...res);
  108. }
  109.  
  110. console.clear();
  111.  
  112. console.log(getBalancedLength(''));
  113. console.log(getBalancedLength('(a)'));
  114. console.log(getBalancedLength(')('));
  115. console.log(getBalancedLength('()'));
  116. console.log(getBalancedLength('))(()()))'));
  117. console.log(getBalancedLength('()())()((()))'));
  118. </script></body>
  119. </html>
Add Comment
Please, Sign In to add comment