Java Substring Comparisons

  • + 0 comments

    import java.util.Scanner;

    public class Solution {

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = "";
        String largest = "",s2;
        int j=0,s1=0,e=k;
        int n=s.length()-k+1;
        String []ss=new String[s.length()-k+1];
        while(j!=(s.length()-k+1)){
            ss[j]=s.substring(s1,e);
            s1++;
            e++;
            j++;
        }
        for(int i=0;i<n-1;i++){
            for(j=i+1;j<n;j++){
                if(ss[i].compareTo(ss[j])>0){
            s2=ss[i];
                    ss[i]=ss[j];
                    ss[j]=s2;
    
                }
            }
        }
        smallest=ss[0];
        largest=ss[n-1];
        return smallest + "\n" + largest;
    }