Wednesday, April 15, 2020

Dictionary order

Dictionary order (100 Marks)

You will be given an array of strings and you are asked to sort them lexicographically. Note: the length of the array should not be less than 2.

Input Format
You will be given a function with array of strings as single argument. 



Constraints

Size of array <= 10000



Output Format

You need to return the reverse lexicographically sorted array from the function.

Sample TestCase 1
Input

4
dky
amit
sumit
deepak
Output
amit
deepak
dky
sumit
JDK 8



















import java.io.*;
import java.util.*;
import jav.util.stream.*;
//import java.util.stream.*;
public class CandidateCode {

       public static void main(String args[] ) throws Exception {
       Scanner sc = new Scanner(System.in);
       int size=sc.nextInt();
       ArrayList<String> arr = new ArrayList<String>(size);

      for(int i=0 ;i<size; i++)
            arr.add(sc.next());

      List<String> list = arr;
      List<String> slist = list.stream().sorted().collect(Collectors.toList());

      for(int i=0; i<size; i++)
         if(i!=size-1)
            System.out.println(slist.get(i));
        else
            System.out.print(slist.get(i));
     }
}

No comments:

Post a Comment