Rearrange the string
Clash Royale CLAN TAG#URR8PPP
Rearrange the string
For Example-
input1 String ="2a[2b[c]]"
then output should be "abcbcabcbc
"
String ="2a[2b[c]]"
abcbcabcbc
input2 String ="2a[b[c]]"
then output should be "abcabc
"
String ="2a[b[c]]"
abcabc
import java.util.*;
class a
public static void main(String args)
String s = "2a[2b[c]]]";
// o/p = abcbcabcbc
Stack s1 = new Stack();
for(int i=0;i<s.length();i++)
s1.push(s.charAt(i));
String str="";
String sss="";
for(int j=0;j<s.length();j++)
char a = (char)s1.pop();
// if(a == '')
if((int)a >=49 && (int)a<=58)
for(int i=0;i<(int)a-48;i++)
sss+=str;
System.out.println(sss);
else if((int)a >=97 && (int)a <= 122)
str = a + str;
System.out.println(str);
System.out.println(sss);
but my output is showing like "bcbcabcabc".
"2a[2b[c]]"
"abcbcabcbc"
This is not an explanation, just another example. Currently your question is not clear enough to help you.
– Guy
Aug 8 at 8:14
2a[2b[c]] it should print 2 times of abc and 2 times of bc like abcbc , abcbc and o/p be abcbcabcbc
– arun kumar
Aug 8 at 8:22
1 Answer
1
Before you do sss+=str;
,please add sss=str;
.
you forget to update your sss
.
sss+=str;
sss=str;
sss
public static void main(String args)
String s = "2a[2b[c]]]";
Stack s1 = new Stack();
for(int i=0;i<s.length();i++)
s1.push(s.charAt(i));
String str="";
String sss="";
for(int j=0;j<s.length();j++)
char a = (char)s1.pop();
System.out.println("a:"+a);
// if(a == '')
if((int)a >=49 && (int)a<=58)
for(int i=0;i<(int)a-48;i++)
sss=str;
sss+=str;
System.out.println("sss:"+sss);
str=sss;
//character
else if((int)a >=97 && (int)a <= 122)
str = a + str;
System.out.println("str:"+str);
System.out.println(sss);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please explain what is the logic to get from
"2a[2b[c]]"
to"abcbcabcbc"
.– Guy
Aug 8 at 8:10