The question provides an input String and an integer, it asks us to convert the string into all the possible combinations of sub-strings of length as specified by the input integer provided. Then we have to find the maximum and minimum from those sub-strings.
I found a solution for the problem but can anyone please explain the looping part. The Solution:
import java.io.*;`
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int k = sc.nextInt();
String maxString = s.substring(0, k);
String minString = s.substring(0, k);
for (int i = 1; i <= s.length() - k; ++i){
if (maxString.compareTo(s.substring(i, i + k)) < 0)
maxString = s.substring(i, i + k);
if (minString.compareTo(s.substring(i, i + k)) > 0)
minString = s.substring(i, i + k);
}
System.out.println(minString);
System.out.println(maxString);
}
}