NadezhdaGeorgieva

07. String Explosion

Dec 12th, 2020
580
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package bg.softuni.javafundamentals;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class E07StringExplosion2 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         StringBuilder sb = new StringBuilder(scanner.nextLine());
  9.  
  10.         int strengthOfExplosion = 0;
  11.  
  12.         for (int i = 0; i < sb.length(); i++) {
  13.             if (sb.charAt(i) == '>') {
  14.                 int a = Character.getNumericValue(sb.charAt(i + 1));
  15.                 strengthOfExplosion += a;
  16.             } else {
  17.                 if (strengthOfExplosion > 0) {
  18.                     sb.deleteCharAt(i);
  19.                     strengthOfExplosion--;
  20.                     i--; // return to the previous symbol
  21.                 }
  22.             }
  23.         }
  24.         System.out.println(sb);
  25.     }
  26. }
  27.  
RAW Paste Data