Java Substring Comparisons

  • + 0 comments
    public static String getSmallestAndLargest(String s, int k) {
        String smallest = s;
        String largest = "";
    
        for(int i=0; i<s.length()-k+1; i++){
            String subString = s.substring(i,(i+k));
    
            if (subString.compareTo(smallest) < 0){
                smallest = subString;
            }
            if (subString.compareTo(largest) >= 0){
                largest = subString;
            }
        }
    
        return smallest + "\n" + largest;
    }