fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static String reverseParentheses(String s) {
  11. StringBuilder str = new StringBuilder(s);
  12. if(s.contains("(")){
  13. while(str.indexOf("(") != (-1)){
  14. int begin = str.lastIndexOf("(");
  15. int end = str.indexOf(")");
  16. String t = reverse(str.substring(begin + 1, end));
  17. int count = 0;
  18. for(int i = begin + 1; i < end;i++){
  19. str.setCharAt(i, t.charAt(count++));
  20. }
  21. str.setCharAt(begin,' ');
  22. str.setCharAt(end, ' ');
  23. }
  24. System.out.println(str);
  25. return str.toString().replaceAll(" ", "");
  26. }else{
  27. return reverse(s);
  28. }
  29.  
  30. }
  31. public static String reverse(String s){
  32. return new StringBuffer(s).reverse().toString();
  33. }
  34.  
  35. public static void main (String[] args) throws java.lang.Exception
  36. {
  37. System.out.println(reverseParentheses("a(ab)(dc)"));
  38. }
  39. }
Runtime error #stdin #stdout #stderr 0.05s 711168KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
	at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:908)
	at java.lang.StringBuilder.substring(StringBuilder.java:76)
	at Ideone.reverseParentheses(Main.java:16)
	at Ideone.main(Main.java:37)