fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13.  
  14. System.out.print("Entre com o comando: ");
  15. String entrada = sc.nextLine();
  16.  
  17. ArrayList<String> posicoes = new ArrayList<String>(Arrays.asList(entrada.toUpperCase().split("")));
  18.  
  19.  
  20. //Se tiver um X e antes do tiver uma letra remove o X e a Letra:
  21. for (int i = 1; i < posicoes.size(); i++) {
  22. if (posicoes.get(i).equalsIgnoreCase("X") && posicoes.get(i-1).matches("[A-Z]*")){
  23. posicoes.remove(i);
  24. posicoes.remove(i - 1 );
  25. }
  26. }
  27.  
  28. for (int j = posicoes.size()-1; j > 0 ; j--) {
  29. if (posicoes.get(j).equalsIgnoreCase("X") && posicoes.get(j-1).matches("[A-Z]*")){
  30. posicoes.remove(j);
  31. posicoes.remove(j - 1 );
  32. j -= 1;
  33. }
  34.  
  35. }
  36.  
  37. System.out.println(String.join("", posicoes));
  38. }
  39. }
Success #stdin #stdout 0.13s 51476KB
stdin
AABBXX
stdout
Entre com o comando: AA