[Java] Reverse content in brackets

import java.util.Stack;

public class Solution {
static String strFinal = "";
static Integer reversedInteger ;
public static void main(String[] args) {
String result = reversedString("Hello(We(lc)ome)");
System.out.println(result);
}
static String reversedString (String input){
StringBuilder s = new StringBuilder(input);
for (int i=0; i<s.length(); i++) {
if(s.charAt(i) == '(') {
strFinal = strFinal + stackReverse(input,i+1);
i = reversedInteger;
}else if (s.charAt(i) == ')');
else {
strFinal = strFinal+ s.charAt(i);
}
}
return strFinal;
}

static String stackReverse (String S, Integer index){
Stack<Character> temporarystack = new Stack();
String reversedStringByStack = "";
for (int i=index; i<S.length(); i++) {
if (S.charAt(i) == '(') {
StringBuilder reversed = new StringBuilder(stackReverse(S, i+1));
for(int k =0; k<reversed.length();k++){
temporarystack.push(reversed.charAt(k));
}
i = reversedInteger;
}
else if (S.charAt(i) == ')') {
reversedInteger = i;
break;
} else {
temporarystack.push(S.charAt(i));
}
}
while (!temporarystack.isEmpty()){
reversedStringByStack = reversedStringByStack + temporarystack.pop();
}
return reversedStringByStack;
}
}

--

--

--

Techie | Traveler | Writer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dilshani Subasinghe

Dilshani Subasinghe

Techie | Traveler | Writer

More from Medium

Journey of DLithe Bootcamp JAVA Full Stack Developer||Week5(April18-April23)

Immutability Concept In Java

Arrays and ArrayList in Java

A trip to Objectiville -OOP Chapter -2