I have a string s, and an integer k (substring length), I am trying to write the function so that it finds the lexicographically smallest and largest substrings of length k. And It returns a String where samllest and largest substring combined with a newline.
Till now I solved the problem with below approach where I wrote same code to find both smallest and largest substring, however, I want to return both substring with single line of code with stream.
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
smallest = IntStream.range(0, s.length() - k + 1).mapToObj((value) -> s.substring(value, value + k))
.collect(Collectors.minBy(String.CASE_INSENSITIVE_ORDER)).get();
largest = IntStream.range(0, s.length() - k + 1).mapToObj((value) -> s.substring(value, value + k))
.collect(Collectors.maxBy(String.CASE_INSENSITIVE_ORDER)).get();
return smallest + "\n" + largest;
}
I appreciate any kind of suggestion as I am learning lambda and stream now.
So,how can I solve this simple problem elegantly?