Java Substring Comparisons

  • + 0 comments

    public static String getSmallestAndLargest(String s, int k) {

                String smallest = "";
            String largest = "";
        java.util.List<String> arr = new java.util.ArrayList<>();
    
        int l = s.length() - k;
    
        for (int i = 0; i <= l; i++) {
            String c = s.substring(i, i+k);
            arr.add(c);
        }
        java.util.Collections.sort(arr);
        smallest = arr.get(0);
        largest = arr.get(arr.size() - 1);
    
    
        // Complete the function
        // 'smallest' must be the lexicographically smallest substring of length 'k'
        // 'largest' must be the lexicographically largest substring of length 'k'
    
        return smallest + "\n" + largest;
    }