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.
publicclassJavaSubstringComparisons{publicstaticvoidmain(String[]args){System.out.println(getSmallestAndLargest("welcometojava",14));}// Gets the smallest and largest substring with length k from a string spublicstaticStringgetSmallestAndLargest(Strings,intk){if(k<=s.length()){StringsmallestSubstring=s.substring(0,k);StringlargestSubstring="";for(inti=0;i<=s.length()-k;i++){Stringsubstring=s.substring(i,i+k);if(substring.compareTo(smallestSubstring)<0){smallestSubstring=substring;}if(substring.compareTo(largestSubstring)>0){largestSubstring=substring;}}returnsmallestSubstring+"\n"+largestSubstring;}else{return"Substring length cannot be greater than string length";}}}
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 →