Java Substring Comparisons

Sort by

recency

|

1740 Discussions

|

  • + 0 comments

    Scanner scan = new Scanner(System.in); String s = scan.nextLine(); int k = Integer.parseInt(scan.nextLine()); int N = s.length()-(k-1); String[] arr = new String[N]; for(int i=0;i

  • + 0 comments

    import java.util.Scanner;

    public class Solution { static String getSmallestAndLargest(String s, int k) { String smallest = s.substring(0,k); String largest = s.substring(0,k); for(int i=1;i<=s.length()-k;i++){ String sub = s.substring(i,i+k); int d = smallest.compareTo(sub); if(d>0) smallest = sub; String new_sub = s.substring(i,i+k); int p = largest.compareTo(new_sub); if(p<0) largest = new_sub;

          }
    
        // 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;
    }
    
    
    
    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
    public static String getSmallestAndLargest(String s, int k) {
        String smallest = "";
        String largest = "";
    
        // Complete the function
        // 'smallest' must be the lexicographically smallest substring of length 'k'
        // 'largest' must be the lexicographically largest substring of length 'k'
        java.util.ArrayList<String> substr = new java.util.ArrayList<>(); // Empty list
        int i=0;
    
        while(i+k<=s.length()){
        substr.add(s.substring(i, i+k));
        i++;
        }
         java.util.Collections.sort(substr);
        smallest = substr.get(0);
        largest = substr.get(substr.size()-1);
        return smallest + "\n" + largest;
    }
    
  • + 1 comment

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

        String smallest = s.substring(0, k);
        String largest = smallest;
    
        for (int x=1; x<=s.length()-k; x++) {
    
            String tempSub = s.substring(x, x+k);
    
            smallest = (tempSub.compareTo(smallest)<0) ? tempSub : smallest;
            largest = (tempSub.compareTo(largest)>0) ? tempSub : largest;
        }
        return smallest + "\n" + largest;
    }
    
    • + 1 comment

      how to think like you efficiently, i tried with twice as many lines and still got largest wrong and smallest right

      • + 1 comment

        Don't laugh, I'm just a beginner too)))). Just keep doing and analyzing other people's good cod. Everything will be great.

        • + 1 comment

          Well i think u will pass time complexity cases very easily, thanks for suggestion

  • + 1 comment

    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

      Why are you making it so complex when it can be done so simply and cleanly.