fork download
  1. @Mathematica
  2.  
  3. flipBrakets[str_]:=str//
  4. FixedPoint[StringReplace[#,"()"->""]&,#]&//
  5. StringReplace[#,{"(("->"1","))"->"1"}]&//
  6. FixedPoint[StringReplace[#,{")1"->"1)","(1"->"1("}]&,#]&//
  7. StringReplace[#,")("->"2"]&//
  8. If[Or[#=="("\[Or]#==")",
  9. -1,
  10. #//
  11. Characters//
  12. Map[ToExpression,#]&//
  13. Total]&;
  14.  
  15. In[1] := flipBrakets["())((())()))()(((()))()((((((((()()(()))"]
  16. Out[1] = 5
  17.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty