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="";String[]subStr=newString[s.length()-(k-1)];subStr[0]=s.substring(0,k);// adding all substrings to a array for(inti=1;i<subStr.length;i++){subStr[i]=s.substring(i,i+k);}// using bubble sort for lexicographically element orderfor(inti=0;i<subStr.length;i++){for(intj=0;j<subStr.length-i-1;j++){if(subStr[j].compareTo(subStr[j+1])>0){Stringtemp=subStr[j];subStr[j]=subStr[j+1];subStr[j+1]=temp;}}}smallest=subStr[0];largest=subStr[subStr.length-1];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 →