Created
March 23, 2020 15:20
-
-
Save goyuninfo/e7332f11cc5c587873e03c05164bd5de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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