Java Substring Comparisons

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

    }