import java util Scanner public class StringExplosion public static vo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
public class StringExplosion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
StringBuilder result = new StringBuilder();
// iterate through the input string
for (int i = 0; i < input.length(); i++) {
char symbol = input.charAt(i);
if (symbol == '>') {
// add ">" to the result
result.append(">");
// go to the next position to take the next char
i++;
// take that next cher and convert it to int - our current power
int power = input.charAt(i) - '0';
/*at the moment we get the power,
we decrease it by one - we "remove" (we don't add it to the result) the digit
*/
power--;
// we have to iterate while the power > 0 and only if we're in range of string's indices
while (power > 0 && i < input.length() - 1) {
// on each iteration we have to increase i by 1 to have the ability to go to the next char
i++;
/* here we have to check if the char at our new index (because we have already increase i)
is '>' again
*/
if (input.charAt(i) == '>') {
result.append(">");
/* in case power > 0 and the current char is '>', we go to the next position to take the power
when power > 0, we have to sum the previous(old) power and the new one
*/
power += input.charAt(i + 1) - '0';
// again we decrease power by one to "remove" the digit
continue;
}
/* if the char at the increased index is not '>', we decrease the value of power
(we change the value of the control variable of the loop)
*/
power--;
}
} else {
// if it's not '>', we should add the symbol to the result StringBuilder
result.append(symbol);
}
}
System.out.println(result);
}
}