Java Substring Comparisons

Sort by

recency

|

1727 Discussions

|

  • + 0 comments

    Solution using JAVA 8

    public static String getSmallestAndLargest(String s, int k){
            List<String> subStrings = 
            IntStream.rangeClosed(0, s.length()-k)
                    .mapToObj(i -> s.substring(i,i+k))
                    .sorted(Comparator.naturalOrder())
                    .collect(Collectors.toList());
                
            String smallest = subStrings.get(0);
            String largest = subStrings.get(subStrings.size()-1);
                    
            return smallest+"\n"+largest;
        }
    
  • + 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;
                }
            }
    
  • + 0 comments

    This can be done without consuming extra space by maintaining an array/list

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

    public static String getSmallestAndLargest(String s, int k){ String smallest = ""; String largest = "";

        String[] arr = new String[s.length()+1-k];
        for(int i =0 ; i<s.length();i++){
            if(i!=s.length()-k){
                arr[i] = s.substring(i, k+i);
            }else{
                arr[i] = s.substring(i, k+i);
                break;
            }
        }
        smallest = arr[0];
        largest = arr[0];
    
        for (int i=1;i< arr.length;i++){
            if(arr[i].compareTo(largest)>0){
                largest = arr[i];
            }
            if (arr[i].compareTo(smallest)<0) {
                smallest= arr[i];
            }
        }
        return smallest + "\n" + largest;
    }
    
  • + 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;
    }