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.
public static String getSmallestAndLargest(String s, int k) {
String smallest = s.substring(0, k);
String largest = smallest;
for (int i = 1; i < s.length() - k + 1; i++){
if (smallest.compareTo(s.substring(i, i +k)) > 0){
smallest = s.substring(i, i + k);
}
else if (largest.compareTo(s.substring(i, i + k)) < 0){
largest = s.substring(i, i + k);
}
}
return smallest + "\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 →