Advertisement
desislava_topuzakova

07. String Explosion

Nov 15th, 2022
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. package text_processing;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class StringExplosion_07 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String input = scanner.nextLine(); //"abv>1>1>2>2asdasd"
  10. StringBuilder textBuilder = new StringBuilder(input);
  11. int totalStrength = 0; //сила
  12. for (int position = 0; position <= textBuilder.length() - 1; position++) {
  13. char currentSymbol = textBuilder.charAt(position);
  14. if (currentSymbol == '>') {
  15. //експлозия
  16. //char ('1') -> "1" -> int 1
  17. int explosionStrength = Integer.parseInt(textBuilder.charAt(position + 1) + ""); //сила на експлозия
  18. totalStrength += explosionStrength;
  19. } else if (currentSymbol != '>' && totalStrength > 0) {
  20. //премахвам
  21. textBuilder.deleteCharAt(position);
  22. totalStrength--;
  23. position--;
  24. }
  25. }
  26.  
  27. System.out.println(textBuilder);
  28.  
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement