Java Substring Comparisons

Sort by

recency

|

1719 Discussions

|

  • + 0 comments

    does my code have any problem, it always failed to test case 3, however when i try the input of test case 3 as custom input, the output is correct.

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;
    
    public class solution {
        static ArrayList<String> subList = new ArrayList<>();
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            int k = sc.nextInt();
            System.out.println(subAllList(s, k));
        }
    
        private static String subAllList(String s, int k) {
            for (int i = 0; i <= s.length() - k; i++) {
                String stringList = s.substring(i, i + k);
                subList.add(stringList);
            }
            Collections.sort(subList);
            String smallest = subList.get(0);
            String largest = subList.get(subList.size() - 1);
            if (smallest.equalsIgnoreCase(largest)) {
                return smallest;
            } else {
                return smallest + "\n" + largest;
            }
        }
    }
    
  • + 0 comments

    Perfectly working, Logic is just that you need to keep checking all the substrings untill compateTo() function filters all the substrings lexographically.

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = s.substring(0,k);
        String largest = s.substring(0,k);
        String dummyString = "";
    
        for (int i=0; i<=s.length()-k; i++){
            dummyString = s.substring(i, i+k);
            if(dummyString.compareTo(largest)>0){
                largest = dummyString;
            }            
            if(dummyString.compareTo(smallest)<0){
                smallest = dummyString;
            }            
        }
        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 = "";
        java.util.List<String> total = new java.util.ArrayList();
    
        // 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()-1;i++)
        {
            try{
            String sort = s.substring(i, k+i);
            total.add(sort); 
            }
            catch(Exception e)
            {
                break;
            }
        }
        java.util.Collections.sort(total);
        smallest = total.get(0);
        largest = total.get(total.size()-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

    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));
    }
    

    }