Skip to content

Instantly share code, notes, and snippets.

@goyuninfo
Created March 23, 2020 15:20
Show Gist options
  • Save goyuninfo/e7332f11cc5c587873e03c05164bd5de to your computer and use it in GitHub Desktop.
Save goyuninfo/e7332f11cc5c587873e03c05164bd5de to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Solution {
public static String getSmallestAndLargest(String str, 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'
java.util.SortedSet<String> sets = new java.util.TreeSet<>();
for (int i = 0; i <= str.length() - k; i++) {
sets.add(str.substring(i, i + k));
}
smallest = sets.first();
largest = sets.last();
return smallest + "\n" + largest;
}
public static void main(String[] args) {
String s;
int k;
try (Scanner scan = new Scanner(System.in)) {
s = scan.next();
k = scan.nextInt();
}
System.out.println(getSmallestAndLargest(s, k));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment