Java Substring Comparisons

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