fork(4) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. String exampleStr = "back-pay) zyzhk1219(17) zyzhk1329 zyzhk1595(15) zyzhk1988 zyzhk2004 zyzhk2131) jswioj((testsjkldf";
  9. System.out.println(exampleStr);
  10.  
  11. String firstPass = "";
  12. int parenLevel = 0;
  13. for (int i = 0; i < exampleStr.length(); i++)
  14. {
  15. char c = exampleStr.charAt(i);
  16. if (c == '(')
  17. {
  18. parenLevel++;
  19. firstPass += c;
  20. }
  21. else if (c == ')')
  22. {
  23. if (parenLevel > 0)
  24. {
  25. parenLevel--;
  26. firstPass += c;
  27. }
  28. }
  29. else
  30. {
  31. firstPass += c;
  32. }
  33. }
  34.  
  35. String secondPass = "";
  36. parenLevel = 0;
  37. for (int i = firstPass.length() - 1; i >= 0; i--)
  38. {
  39. char c = firstPass.charAt(i);
  40. if (c == ')')
  41. {
  42. parenLevel++;
  43. secondPass = c + secondPass;
  44. }
  45. else if (c == '(')
  46. {
  47. if (parenLevel > 0)
  48. {
  49. parenLevel--;
  50. secondPass = c + secondPass;
  51. }
  52. }
  53. else
  54. {
  55. secondPass = c + secondPass;
  56. }
  57. }
  58.  
  59. String result = secondPass;
  60. System.out.println(result);
  61. }
  62. }
Success #stdin #stdout 0.02s 245632KB
stdin
Standard input is empty
stdout
back-pay) zyzhk1219(17) zyzhk1329 zyzhk1595(15) zyzhk1988 zyzhk2004 zyzhk2131) jswioj((testsjkldf
back-pay zyzhk1219(17) zyzhk1329 zyzhk1595(15) zyzhk1988 zyzhk2004 zyzhk2131 jswiojtestsjkldf