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. */
    String ss;
    int k;
    int ct=0;
    Scanner sc=new Scanner(System.in);
    ss=sc.nextLine();
    k=sc.nextInt();
    String[] arr=new String[ss.length()-k+1];
    int st=0,ed=k-1;
    
        int m=0;
        while(m!=(ss.length()-k+1))
        {
    
            arr[m]=ss.substring(st,ed+1);
            st++;
            ed++;
            m++;
        }
        // for(int i=0;i<ss.length()-k;i++)
        // {
        //     System.out.print(arr[i]+" ");
        // }
        String small=arr[0];
        String large=arr[ss.length()-k];
        for(int i=0;i<ss.length()-k+1;i++)
        {
            if(small.compareTo(arr[i])<0)   small=arr[i];
        }
        for(int i=0;i<ss.length()-k+1;i++)
        {
            if(large.compareTo(arr[i])>0)   large=arr[i];
        }
        System.out.println(large+"\n"+small);
    }   
    

    }