Java Substring Comparisons

  • + 0 comments
    public static String getSmallestAndLargest(String s, int k) {
            String smallest = "";
            String largest = "";
            smallest = "z";
            for(int i = 0, j = k; j <= s.length(); i++, j++){
                String str = s.substring(i,j);
                if(str.compareTo(smallest) < 0){
                    smallest = str;
                }if(str.compareTo(largest) > 0 ){
                    largest = str;
                }
            }