Skip to main content
Fix comment
Source Link
EzioMercer
  • 1.5k
  • 2
  • 21

JavaScript, 130 bytes

s=>[...s,0].map((_,i,a)=>a.map((_,j,$,S=s.slice(i,j),b=0)=>{for(l of S)if((l==')'?--b:++b)<0)break;r=b|r>(l=S.length)?r:l}),r=0)|r

Try it:

f=s=>[...s,0].map((_,i,a)=>a.map((_,j,$,S=s.slice(i,j),b=0)=>{for(l of S)if((l==')'?--b:++b)<0)break;r=b|r>(l=S.length)?r:l}),r=0)|r

;[
  '(()())', // 6

  ')()())', // 24

  '()(())', // 6

  '()(()', // 2

  '))', // 0

  '', // 0
].forEach(s=>console.log(f(s)))

Algorithm:

Take the all substrings of string and check if it is balanced parentheses. If yes, then check if the length of substring is maximum? If yes then update the max value. Else don't change the max value

JavaScript, 130 bytes

s=>[...s,0].map((_,i,a)=>a.map((_,j,$,S=s.slice(i,j),b=0)=>{for(l of S)if((l==')'?--b:++b)<0)break;r=b|r>(l=S.length)?r:l}),r=0)|r

Try it:

f=s=>[...s,0].map((_,i,a)=>a.map((_,j,$,S=s.slice(i,j),b=0)=>{for(l of S)if((l==')'?--b:++b)<0)break;r=b|r>(l=S.length)?r:l}),r=0)|r

;[
  '(()())', // 6

  ')()())', // 2

  '()(())', // 6

  '()(()', // 2

  '))', // 0

  '', // 0
].forEach(s=>console.log(f(s)))

Algorithm:

Take the all substrings of string and check if it is balanced parentheses. If yes, then check if the length of substring is maximum? If yes then update the max value. Else don't change the max value

JavaScript, 130 bytes

s=>[...s,0].map((_,i,a)=>a.map((_,j,$,S=s.slice(i,j),b=0)=>{for(l of S)if((l==')'?--b:++b)<0)break;r=b|r>(l=S.length)?r:l}),r=0)|r

Try it:

f=s=>[...s,0].map((_,i,a)=>a.map((_,j,$,S=s.slice(i,j),b=0)=>{for(l of S)if((l==')'?--b:++b)<0)break;r=b|r>(l=S.length)?r:l}),r=0)|r

;[
  '(()())', // 6

  ')()())', // 4

  '()(())', // 6

  '()(()', // 2

  '))', // 0

  '', // 0
].forEach(s=>console.log(f(s)))

Algorithm:

Take the all substrings of string and check if it is balanced parentheses. If yes, then check if the length of substring is maximum? If yes then update the max value. Else don't change the max value

Reduce bytes count
Source Link
EzioMercer
  • 1.5k
  • 2
  • 21

JavaScript, 137130 bytes

s=>evals=>[...s,0].map("for(r=i=0;i<s[k='length'];++i_,i,a)for=>a.map(j=i;S=s(_,j,$,S=s.slice(i,j),b=0,j++<s[k]+1;r=b|r>S[k]?r:S[k])=>{for(l of S)if((l==')'?--b:++b)<0)break;r"break;r=b|r>(l=S.length)?r:l}),r=0)|r

Try it:

f=s=>evalf=s=>[...s,0].map("for(r=i=0;i<s[k='length'];++i_,i,a)for=>a.map(j=i;S=s(_,j,$,S=s.slice(i,j),b=0,j++<s[k]+1;r=b|r>S[k]?r:S[k])=>{for(l of S)if((l==')'?--b:++b)<0)break;r"break;r=b|r>(l=S.length)?r:l}),r=0)|r

;[
  '(()())', // 6

  ')()())', // 2

  '()(())', // 6

  '()(()', // 2

  '))', // 0

  '', // 0
].forEach(s=>console.log(f(s)))

Algorithm:

Take the all substrings of string and check if it is balanced parentheses. If yes, then check if the length of substring is maximum? If yes then update the max value. Else don't change the max value

JavaScript, 137 bytes

s=>eval("for(r=i=0;i<s[k='length'];++i)for(j=i;S=s.slice(i,j),b=0,j++<s[k]+1;r=b|r>S[k]?r:S[k])for(l of S)if((l==')'?--b:++b)<0)break;r")

Try it:

f=s=>eval("for(r=i=0;i<s[k='length'];++i)for(j=i;S=s.slice(i,j),b=0,j++<s[k]+1;r=b|r>S[k]?r:S[k])for(l of S)if((l==')'?--b:++b)<0)break;r")

;[
  '(()())', // 6

  ')()())', // 2

  '()(())', // 6

  '()(()', // 2

  '))', // 0

  '', // 0
].forEach(s=>console.log(f(s)))

Algorithm:

Take the all substrings of string and check if it is balanced parentheses. If yes, then check if the length of substring is maximum? If yes then update the max value. Else don't change the max value

JavaScript, 130 bytes

s=>[...s,0].map((_,i,a)=>a.map((_,j,$,S=s.slice(i,j),b=0)=>{for(l of S)if((l==')'?--b:++b)<0)break;r=b|r>(l=S.length)?r:l}),r=0)|r

Try it:

f=s=>[...s,0].map((_,i,a)=>a.map((_,j,$,S=s.slice(i,j),b=0)=>{for(l of S)if((l==')'?--b:++b)<0)break;r=b|r>(l=S.length)?r:l}),r=0)|r

;[
  '(()())', // 6

  ')()())', // 2

  '()(())', // 6

  '()(()', // 2

  '))', // 0

  '', // 0
].forEach(s=>console.log(f(s)))

Algorithm:

Take the all substrings of string and check if it is balanced parentheses. If yes, then check if the length of substring is maximum? If yes then update the max value. Else don't change the max value

Source Link
EzioMercer
  • 1.5k
  • 2
  • 21

JavaScript, 137 bytes

s=>eval("for(r=i=0;i<s[k='length'];++i)for(j=i;S=s.slice(i,j),b=0,j++<s[k]+1;r=b|r>S[k]?r:S[k])for(l of S)if((l==')'?--b:++b)<0)break;r")

Try it:

f=s=>eval("for(r=i=0;i<s[k='length'];++i)for(j=i;S=s.slice(i,j),b=0,j++<s[k]+1;r=b|r>S[k]?r:S[k])for(l of S)if((l==')'?--b:++b)<0)break;r")

;[
  '(()())', // 6

  ')()())', // 2

  '()(())', // 6

  '()(()', // 2

  '))', // 0

  '', // 0
].forEach(s=>console.log(f(s)))

Algorithm:

Take the all substrings of string and check if it is balanced parentheses. If yes, then check if the length of substring is maximum? If yes then update the max value. Else don't change the max value