Java Substring Comparisons

Sort by

recency

|

1736 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
        try(Scanner s = new Scanner(System.in)){
            //System.out.println("Enter your string");
            String str = s.next();
            //System.out.println("Enter Sub String size or length");
             int len = s.nextInt();
    
          System.out.println(getSmallestAndLargest(str, len));
    
        }
    }
    
    public static String getSmallestAndLargest(final String str, final int l){
        if(Objects.isNull(str) || str.isBlank() || str.length()< l  || l<=0 || str.length()>1000){
            return "";
        }
       List<String> s = IntStream.range(0, str.length()-l+1)
        .mapToObj(i-> str.substring(i, i+l))
        .map(String::trim)
        .distinct()
        .sorted()
        .collect(Collectors.toList());
    
        if(s.size() >2){
           return String.format("%s"+"\n"+"%s", s.get(0), s.get(s.size()-1)); 
        }
    
    
        if(s.size() ==1){
           return String.format("%s"+"\n"+"%s", s.get(0), s.get(0)); 
        }
    
        return "";
    }
    

    }

  • + 0 comments

    ** JAVA SOLUTION **

    import java.util.Scanner;

    public class Solution {

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = s.substring(0, k);
        String largest = s.substring(0,k);
        try{
            for (int j = 1; j < s.length()-(k-1); j++) {
                if(s.substring(j, j+k).compareTo(smallest) < 0){
                    smallest = s.substring(j, j+k);
                }
                else if(s.substring(j, j+k).compareTo(largest) > 0){
                    largest = s.substring(j, j+k);
                }
            }
        }   
        catch (Exception e){
            System.out.println("");
        }
    
        // 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;
    }
    
  • + 1 comment

    import java.util.Scanner;

    public class Solution {

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = s.substring(0, k);
        String largest = s.substring(0, k);
    
        // Complete the function
        // 'smallest' must be the lexicographically smallest substring of length 'k'
        // 'largest' must be the lexicographically largest substring of length 'k'
    
        for (int i = 0; i <= s.length() - k; i++) {
          String myWord = s.substring(i, i + k);
          if (smallest.compareTo(myWord) > 0) {
            smallest = myWord;
          }
          if(largest.compareTo(myWord) < 0) {
            largest = myWord;
          }
        }
    
        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));
    }
    

    }

  • + 0 comments

    Java's Substring Comparisons help find the lexicographically smallest and largest substrings of a given length. It efficiently sorts and compares substrings using string manipulation techniques. Ekbet12

  • + 1 comment

    My solution:

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            
            String s = sc.nextLine();
            int k = sc.nextInt();
            String smallest = "";
            String largest = "";
            
            sc.close();
            
            ArrayList<String> arrayList = new ArrayList<>();
    
            for (int i = 0; i < s.length(); i++) {
    
                if ((i + k) <= s.length()) {
                    arrayList.add(s.substring(i, i + k));
                }
            }
    
            smallest = Collections.min(arrayList);
            largest = Collections.max(arrayList);
    
            System.out.println(smallest + "\n" + largest);
        }