We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I create different solution, but passed tests:
public static String getSmallestAndLargest(String s, int k) {
ArrayList<String> subStr = new ArrayList<>();
try{
for (int i = 0; i < s.length(); i++) {
subStr.add(s.substring(i, i + k));
}
for (int i = s.length(); i == 0; i--) {
subStr.add(s.substring(i, i - k));
}
for (int i = 2; i <= s.length(); i++) {
subStr.add(s.substring(i, i + k));
}
}catch (Exception e){
e.printStackTrace();
}
subStr.sort(null);
return subStr.get(0) + "\n" + subStr.get(subStr.size() - 1);
}
public static void main (String[]args){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int k = sc.nextInt();
System.out.println(getSmallestAndLargest(s, k));
sc.close();
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Substring Comparisons
You are viewing a single comment's thread. Return to all comments →
I create different solution, but passed tests: public static String getSmallestAndLargest(String s, int k) {