Its a program to print the Lexicographically smallest and largest substring of size k.
There´s a part in this solution I don´t really understand. Maybe someone can explain it to me.
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
String currStr = s.substring(0, k);
Here is the part I dont get. why is int i
initialised with k
and how does
for(int i = k; i<s.length(); i++){
currStr = currStr.substring(1, k) + s.charAt(i);
exactly work?
Full loop:
for(int i = k; i<s.length(); i++){
currStr = currStr.substring(1, k) + s.charAt(i);
if (lexMax.compareTo(currStr) < 0)
lexMax = currStr;
if (lexMin.compareTo(currStr) > 0)
lexMin = currStr;
}
return smallest + "\n" + largest;
}