saurav_kalsoor

Help Mocha to Sort - JAVA

Nov 18th, 2021 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Test{
  4.  
  5.     static Scanner sc = new Scanner(System.in);
  6.     public static void main(String[] args) {
  7.         int n = sc.nextInt();
  8.         String s = sc.next();
  9.         helpMochaToSort(n, s);
  10.     }
  11.  
  12.     public static void helpMochaToSort(int n, String s) {
  13.  
  14.         ArrayList<Integer> res = new ArrayList<Integer>();
  15.         char[] temp = s.toCharArray();
  16.         Arrays.sort(temp);
  17.         for(int i=0; i < n; i++){
  18.             if(s.charAt(i) != temp[i])
  19.                 res.add(i);
  20.         }
  21.  
  22.         if(res.isEmpty()){
  23.             System.out.println(0);
  24.             return;
  25.         }
  26.  
  27.         System.out.println(1);
  28.         System.out.print(res.size() + " ");
  29.        
  30.         for(int a : res)
  31.             System.out.print(a + " ");
  32.         System.out.println();
  33.     }
  34.  
  35. }
Add Comment
Please, Sign In to add comment