Java Substring Comparisons

Sort by

recency

|

1721 Discussions

|

  • + 0 comments

    public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String test = scanner.nextLine();
    
        int tests = scanner.nextInt();
       System.out.println(getSmallestAndLargest(test, tests));
    }
    public static String getSmallestAndLargest(String s, int l){
          List<String> res = new ArrayList<>();
          int condition = s.length() - l;
        for (int i = 0; i <= condition; i++) {
            String temp  = s;
            String c = s.substring(0 , l);
            res.add(c);
            s = temp.substring(1);
        }
        Collections.sort(res);
        return res.get(0) + "\n" + res.get(res.size() - 1);
    }
    

    }

  • + 0 comments
    import java.io.*;
    import java.util.*;
    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. */
            Scanner scan=new Scanner(System.in);
            String s;
            s=scan.nextLine();
            int k=scan.nextInt();
            String small=s.substring(0,k);
            String large=s.substring(0,k);
            for(int i=1; i<=s.length()-k; i++)
            {
                String sub=s.substring(i,i+k);
                if(sub.compareTo(small)<0){
                    small=sub;
                }
                if(sub.compareTo(large)>0){
                    large=sub;
                }
            }
            System.out.println(small+"\n"+large);
            scan.close();
        }
    }
    
  • + 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));
    }
    

    }