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.
importjava.util.Scanner;publicclassSolution{publicstaticStringgetSmallestAndLargest(Strings,intk){Stringsmallest=s.substring(0,k);Stringlargest=s.substring(0,k);// Complete the function// 'smallest' must be the lexicographically smallest substring of length 'k'// 'largest' must be the lexicographically largest substring of length 'k'for(inti=0;i<s.length()-k+1;i++){Stringsub=s.substring(i,i+k);if(sub.compareTo(largest)>0){largest=sub;}if(sub.compareTo(smallest)<0){smallest=sub;}}returnsmallest+"\n"+largest;}publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);Strings=scan.next();intk=scan.nextInt();scan.close();System.out.println(getSmallestAndLargest(s,k));}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Substring Comparisons
You are viewing a single comment's thread. Return to all comments →