Java Substring Comparisons

Sort by

recency

|

1716 Discussions

|

  • + 0 comments

    works perfectly

    public class solution{ public static String getSmallestAndLargest(String s, int k) { String smallest = s.substring(0,k); String largest = s.substring(0,k); int i; for(i = 1 ; i<=s.length() - k ; i++){ String comp = s.substring(i,i+k); if(comp.compareTo(smallest)<0){ smallest = comp; } if(comp.compareTo(largest)>0){ largest = comp; } 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));
    

    }

        return smallest + "\n" + largest;
    }
    
  • + 0 comments

    import java.util.Scanner;

    public class Solution {

    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 stpsize=1;
        int n=s.length()-k+1;
        String []res=new String[n];
        for(int i=0;i<n;i++)
            res[i]=s.substring(i,i+k);
    
         for (int i = 0; i < res.length - 1; i++) {
            for (int j = 0; j < res.length - i - 1; j++) {
                if (res[j].compareTo(res[j + 1]) > 0) {
                    String temp = res[j];
                    res[j] = res[j + 1];
                    res[j + 1] = temp;
                }
            }
        }
        smallest=res[0];
        largest=res[res.length - 1];
        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
    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);
            String compare = "";
            for(int i = 0; i <= s.length()-k; i++){
                compare = s.substring(i,i+k);
                if(compare.compareTo(smallest)<0){
                    smallest = compare;
                }
                if(compare.compareTo(largest)>0){
                    largest = compare;
                }
            }
            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

    Solution

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

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

        for(int i=0;i<s.length()-k;i++){
            String current = s.substring(i,k+i);
            if(current.compareTo(smallest)<0){
                smallest = current;
            }
    
            if(current.compareTo(largest)>0){
                largest = current;
            }
        }
    
        return smallest + "\n" + largest;
    }