Java Substring Comparisons

  • + 0 comments
    import java.io.*;
    import java.util.*;
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner scan=new Scanner(System.in);
            String s;
            s=scan.nextLine();
            int k=scan.nextInt();
            String small=s.substring(0,k);
            String large=s.substring(0,k);
            for(int i=1; i<=s.length()-k; i++)
            {
                String sub=s.substring(i,i+k);
                if(sub.compareTo(small)<0){
                    small=sub;
                }
                if(sub.compareTo(large)>0){
                    large=sub;
                }
            }
            System.out.println(small+"\n"+large);
            scan.close();
        }
    }