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.
publicstaticStringgetSmallestAndLargest(Strings,intk){Stringsmallest="";Stringlargest="";// Complete the function// 'smallest' must be the lexicographically smallest substring of length 'k'// 'largest' must be the lexicographically largest substring of length 'k'String[]a=newString[s.length()-(k-1)];largest="\0";a[0]=s.substring(0,k);smallest=a[0];for(inti=0;i<s.length()-(k-1);i++){a[i]=s.substring(i,i+k);if(largest.compareTo(a[i])<0){largest=a[i];}if(smallest.compareTo(a[i])>0){smallest=a[i];}}returnsmallest+"\n"+largest;}
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 →