Skip to main content
edited tags; edited title; added 74 characters in body
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

How can optimize this reverseParentheses algorithm written in java? Reversing strings inside parentheses

How can I optimize this algorithm to reverse strings inside parentheses?

String reverseParentheses(String s) {
    String tmpCh = new String("");
    String tmpChRe = new String("");
    String tmp = new String("");
    int l = s.length();
    int n = 0;
    int j =0;
    for(int i = 0;i<l;i++){
            if(s.charAt(i)=='(')
                    n++;
    }
    int T[] = new int[n];
    for(int i=0;i<l;i++){
            if(s.charAt(i)=='('){
                    T[j]=i;
                    j++;
            }
    }
    j=0;
    while(n>0){
            j = T[n-1] + 1;
            while(s.charAt(j)!=')'){
                    tmpCh = tmpCh + s.charAt(j);
                    j++;
            }
            for(int q=tmpCh.length()-1;q>=0;q--)
                    tmpChRe = tmpChRe + tmpCh.charAt(q);
            tmp = s.substring(0,T[n-1]) + tmpChRe + s.substring(T[n-1]+tmpChRe.length()+2);
            s = tmp;
            n--;
            tmp = "";
            tmpCh = "";
            tmpChRe = "";
    }
    return s ;
}


    
    

How can optimize this reverseParentheses algorithm written in java?

String reverseParentheses(String s) {
    String tmpCh = new String("");
    String tmpChRe = new String("");
    String tmp = new String("");
    int l = s.length();
    int n = 0;
    int j =0;
    for(int i = 0;i<l;i++){
            if(s.charAt(i)=='(')
                    n++;
    }
    int T[] = new int[n];
    for(int i=0;i<l;i++){
            if(s.charAt(i)=='('){
                    T[j]=i;
                    j++;
            }
    }
    j=0;
    while(n>0){
            j = T[n-1] + 1;
            while(s.charAt(j)!=')'){
                    tmpCh = tmpCh + s.charAt(j);
                    j++;
            }
            for(int q=tmpCh.length()-1;q>=0;q--)
                    tmpChRe = tmpChRe + tmpCh.charAt(q);
            tmp = s.substring(0,T[n-1]) + tmpChRe + s.substring(T[n-1]+tmpChRe.length()+2);
            s = tmp;
            n--;
            tmp = "";
            tmpCh = "";
            tmpChRe = "";
    }
    return s ;
}


    
    

Reversing strings inside parentheses

How can I optimize this algorithm to reverse strings inside parentheses?

String reverseParentheses(String s) {
    String tmpCh = new String("");
    String tmpChRe = new String("");
    String tmp = new String("");
    int l = s.length();
    int n = 0;
    int j =0;
    for(int i = 0;i<l;i++){
            if(s.charAt(i)=='(')
                    n++;
    }
    int T[] = new int[n];
    for(int i=0;i<l;i++){
            if(s.charAt(i)=='('){
                    T[j]=i;
                    j++;
            }
    }
    j=0;
    while(n>0){
            j = T[n-1] + 1;
            while(s.charAt(j)!=')'){
                    tmpCh = tmpCh + s.charAt(j);
                    j++;
            }
            for(int q=tmpCh.length()-1;q>=0;q--)
                    tmpChRe = tmpChRe + tmpCh.charAt(q);
            tmp = s.substring(0,T[n-1]) + tmpChRe + s.substring(T[n-1]+tmpChRe.length()+2);
            s = tmp;
            n--;
            tmp = "";
            tmpCh = "";
            tmpChRe = "";
    }
    return s ;
}


    
    
Fixed code indent
Source Link
forsvarir
  • 11.8k
  • 7
  • 39
  • 72

How can optimize this reverseParentheses algorithm written in java  ?

String reverseParentheses(String s) {
    String tmpCh = new String("");
    String tmpChRe = new String("");
    String tmp = new String("");
    int l = s.length();
    int n = 0;
    int j =0;
    for(int i = 0;i<l;i++){
            if(s.charAt(i)=='(')
                    n++;
    }
    int T[] = new int[n];
    for(int i=0;i<l;i++){
            if(s.charAt(i)=='('){
                    T[j]=i;
                    j++;
            }
    }
    j=0;
    while(n>0){
            j = T[n-1] + 1;
            while(s.charAt(j)!=')'){
                    tmpCh = tmpCh + s.charAt(j);
                    j++;
            }
            for(int q=tmpCh.length()-1;q>=0;q--)
                    tmpChRe = tmpChRe + tmpCh.charAt(q);
            tmp = s.substring(0,T[n-1]) + tmpChRe + s.substring(T[n-1]+tmpChRe.length()+2);
            s = tmp;
            n--;
            tmp = "";
            tmpCh = "";
            tmpChRe = "";
    }
    return s ;
}


    
    

}

How can optimize this reverseParentheses algorithm written in java  ?

String reverseParentheses(String s) {
    String tmpCh = new String("");
    String tmpChRe = new String("");
    String tmp = new String("");
    int l = s.length();
    int n = 0;
    int j =0;
    for(int i = 0;i<l;i++){
            if(s.charAt(i)=='(')
                    n++;
    }
    int T[] = new int[n];
    for(int i=0;i<l;i++){
            if(s.charAt(i)=='('){
                    T[j]=i;
                    j++;
            }
    }
    j=0;
    while(n>0){
            j = T[n-1] + 1;
            while(s.charAt(j)!=')'){
                    tmpCh = tmpCh + s.charAt(j);
                    j++;
            }
            for(int q=tmpCh.length()-1;q>=0;q--)
                    tmpChRe = tmpChRe + tmpCh.charAt(q);
            tmp = s.substring(0,T[n-1]) + tmpChRe + s.substring(T[n-1]+tmpChRe.length()+2);
            s = tmp;
            n--;
            tmp = "";
            tmpCh = "";
            tmpChRe = "";
    }
    return s ;

}

How can optimize this reverseParentheses algorithm written in java?

String reverseParentheses(String s) {
    String tmpCh = new String("");
    String tmpChRe = new String("");
    String tmp = new String("");
    int l = s.length();
    int n = 0;
    int j =0;
    for(int i = 0;i<l;i++){
            if(s.charAt(i)=='(')
                    n++;
    }
    int T[] = new int[n];
    for(int i=0;i<l;i++){
            if(s.charAt(i)=='('){
                    T[j]=i;
                    j++;
            }
    }
    j=0;
    while(n>0){
            j = T[n-1] + 1;
            while(s.charAt(j)!=')'){
                    tmpCh = tmpCh + s.charAt(j);
                    j++;
            }
            for(int q=tmpCh.length()-1;q>=0;q--)
                    tmpChRe = tmpChRe + tmpCh.charAt(q);
            tmp = s.substring(0,T[n-1]) + tmpChRe + s.substring(T[n-1]+tmpChRe.length()+2);
            s = tmp;
            n--;
            tmp = "";
            tmpCh = "";
            tmpChRe = "";
    }
    return s ;
}


    
    
Source Link

How can optimize this reverseParentheses algorithm written in java ?

String reverseParentheses(String s) {
    String tmpCh = new String("");
    String tmpChRe = new String("");
    String tmp = new String("");
    int l = s.length();
    int n = 0;
    int j =0;
    for(int i = 0;i<l;i++){
            if(s.charAt(i)=='(')
                    n++;
    }
    int T[] = new int[n];
    for(int i=0;i<l;i++){
            if(s.charAt(i)=='('){
                    T[j]=i;
                    j++;
            }
    }
    j=0;
    while(n>0){
            j = T[n-1] + 1;
            while(s.charAt(j)!=')'){
                    tmpCh = tmpCh + s.charAt(j);
                    j++;
            }
            for(int q=tmpCh.length()-1;q>=0;q--)
                    tmpChRe = tmpChRe + tmpCh.charAt(q);
            tmp = s.substring(0,T[n-1]) + tmpChRe + s.substring(T[n-1]+tmpChRe.length()+2);
            s = tmp;
            n--;
            tmp = "";
            tmpCh = "";
            tmpChRe = "";
    }
    return s ;

}