We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicclassSolution{staticString[]bigSorting(String[]unsorted){Arrays.sort(unsorted,(x,y)->x.length()==y.length()?x.compareTo(y):Integer.compare(x.length(),y.length()));returnunsorted;}privatestaticfinalScannerscanner=newScanner(System.in);publicstaticvoidmain(String[]args){intn=scanner.nextInt();scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");String[]unsorted=newString[n];for(inti=0;i<n;i++){StringunsortedItem=scanner.next();// nextLine() is too longunsorted[i]=unsortedItem;}String[]result=bigSorting(unsorted);for(inti=0;i<result.length;i++){System.out.print(result[i]);if(i!=result.length-1){System.out.println();}}System.out.println();scanner.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Big Sorting
You are viewing a single comment's thread. Return to all comments →
Test6: Timeout (Java)
When you just return unsorted array to the output:
you still have timeout problem.
To solve this problem just replace
scanner.nextLine()
toscanner.next()
.Full Java solution is: