Java Substring Comparisons

Sort by

recency

|

1766 Discussions

|

  • + 0 comments

    I create different solution, but passed tests: public static String getSmallestAndLargest(String s, int k) {

        ArrayList<String> subStr = new ArrayList<>();
        try{
            for (int i = 0; i < s.length(); i++) {
                subStr.add(s.substring(i, i + k));
            }
            for (int i = s.length(); i == 0; i--) {
                subStr.add(s.substring(i, i - k));
            }
            for (int i = 2; i <= s.length(); i++) {
                subStr.add(s.substring(i, i + k));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    
        subStr.sort(null);
        return subStr.get(0) + "\n" + subStr.get(subStr.size() - 1);
    }
    
        public static void main (String[]args){
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            int k = sc.nextInt();
    
            System.out.println(getSmallestAndLargest(s, k));
    
            sc.close();
    
        }
    
  • + 0 comments

    public static String getSmallestAndLargest(String s, int k) { String smallest = s.substring(0, k); String largest = s.substring(0, k);

    // Loop through all possible substrings of length k
    for (int i = 1; i <= s.length() - k; i++) {
        String sub = s.substring(i, i + k);
    
        if (sub.compareTo(smallest) < 0) {
            smallest = sub;
        }
        if (sub.compareTo(largest) > 0) {
            largest = sub;
        }
    }
    
    return smallest + "\n" + largest;
    

    }

  • + 0 comments

    I propose an optimized guided search with sub-strings' starting points' preselection. After getting the smallest and the largest characters in the input string, I use them as guides that lead the iteration through the preselected category of sub-strings by repeating evaluation of the expression pos = str.lastIndexOf(cat, pos - 1) , where pos becomes a "guided reverse itarator" through the category.

    To get rid of the error-prone repetitions, I abstracted the search code with comparator passed as argument, together with the category mark to guide the serch in the given order.

    Implementation:

    import java.util.Comparator;
    import java.util.Scanner;
    
    public class Solution {
        private static String
          findSubstring(String str, int len, char cat, Comparator<String> cmp) {
            int pos = str.lastIndexOf(cat, str.length() - len);
            String sub_str = str.substring(pos, pos + len);
            pos = str.lastIndexOf(cat, pos - 1);
            while (pos >= 0) {
                if (cmp.compare(str.substring(pos, pos + len), sub_str) < 0)
                    sub_str = str.substring(pos, pos + len);
                pos = str.lastIndexOf(cat, pos - 1);
            }
            return sub_str;
        }
        private static String getSmallestAndLargest(String str, int k) {
            // O: Substring index candidates' preselection:
            // Find the smallest and the largest character in the string
            // to guide the search for the smallest/largest substring.
            int end = str.length() - k + 1;
            char min = 'z'; char max = 'A';
            for (char c : str.substring(0, end).toCharArray()) {
                if (c < min) min = c;
                if (c > max) max = c;
            }
            // Smallest substring
            Comparator<String> less = Comparator.naturalOrder();        
            String sub_min = findSubstring(str, k, min, less);
            // Largest substring
            Comparator<String> greater = Comparator.reverseOrder();        
            String sub_max = findSubstring(str, k, max, greater);    
            return sub_min + sub_max;
        }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            int k = sc.nextInt();
            sc.close();
            String min_max = getSmallestAndLargest(s, k);
            System.out.println(min_max.substring(0, k));
            System.out.println(min_max.substring(k, k * 2));
        }
    }
    
  • + 0 comments

    import java.util.Scanner;

    public class Solution {

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = s.substring(0,k);
        String largest = smallest;
    
        for(int i = 0 ; i<=s.length()-k;i++){
            String subString = s.substring(i,i+k);
            if(subString.compareTo(smallest)<0){smallest = subString;}
            if(subString.compareTo(largest)>0){largest = subString;}
        }
    
        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'
    int n=s.length();
    smallest=s.substring(0,k);
    largest=s.substring(0,k);
    
    for(int i=0;i<n-k;i++) //selection sort
    {
        String sm = s.substring(i,i+k);
        String lg = s.substring(i,i+k);
        for(int j=i+1;j<n+1-k;j++) {
            String t = s.substring(j,j+k);
            if(t.compareTo(sm)<=0) 
                sm=t;
            if(t.compareTo(lg)>=0)
                lg = t;
        }
        if(sm.compareTo(smallest)<=0)
            smallest=sm;
        if(lg.compareTo(largest)>=0)
            largest=lg;
    }
    
    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));
    

    }