I am getting a different value when I am performing the iteration manually on
"welcometojava"
when the value of i=5
I am getting following value of variable
when i=5;
substring =MET
smallest="com"
largest="ome"
At this point, the value that I am getting is not validating both the if conditions
I want help with the values of all 3 variables on each iteration so that I can know where I am getting
wrong.Thankyou
public class Solution {
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
smallest = largest = s.substring(0, k);
for (int i=1; i<s.length()-k+1; i++) {
String substr = s.substring(i, i+k);
if (smallest.compareTo(substr) > 0)
smallest = substr;
if (largest.compareTo(substr) < 0)
largest = substr;
}
return smallest + "\n" + largest;
}