Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. Scanner sc = new Scanner(System.in);
  4. int T = sc.nextInt();
  5. char temp;
  6. int conta = 0;
  7.  
  8. for (int f = 0; f < T; f++) {
  9.  
  10. String sequenza = sc.next();
  11. char[] seq = sequenza.toCharArray();
  12. int fineIndice = seq.length -1;
  13.  
  14. if (seq.length <= 3) {
  15. System.out.println(0);
  16. }
  17. else
  18. {
  19. while(seq[0] == seq[fineIndice]) {
  20. fineIndice--;
  21. }
  22. for (int i = 1; i < seq.length; i++) {
  23. for (int j = i; j < fineIndice; j++) {
  24.  
  25. if (seq[i - 1] == seq[j]) {
  26. i++;
  27. temp = seq[i - 1];
  28. seq[i - 1] = seq[j];
  29. seq[j] = temp;
  30. conta++;
  31. }
  32.  
  33. }
  34. }
  35. }
  36. System.out.println(conta);
  37. conta = 0;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement