Java Substring Comparisons

  • + 0 comments

    works perfectly

    public class solution{ public static String getSmallestAndLargest(String s, int k) { String smallest = s.substring(0,k); String largest = s.substring(0,k); int i; for(i = 1 ; i<=s.length() - k ; i++){ String comp = s.substring(i,i+k); if(comp.compareTo(smallest)<0){ smallest = comp; } if(comp.compareTo(largest)>0){ largest = comp; } return smallest +"\n"+largest; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.next(); int k = scan.nextInt(); scan.close();

    System.out.println(getSmallestAndLargest(s, k));
    

    }

        return smallest + "\n" + largest;
    }