-
Notifications
You must be signed in to change notification settings - Fork 22
/
soln.java
36 lines (28 loc) · 1.08 KB
/
soln.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;
public class Solution {
public static String getSmallestAndLargest(String s, int k) {
String substring = s.substring(0, k);
String smallest = substring;
String largest = substring;
for (int i = 1; i <= s.length() - k; i++) {
// Create a substring of length 'k'
substring = s.substring(i, i + k);
// If current substring is lexicographically smaller than 'smallest'
if (substring.compareTo(smallest) < 0) {
smallest = substring;
}
// If current substring is lexicographically larger than 'largest'
if (substring.compareTo(largest) > 0) {
largest = substring;
}
}
return smallest + "\n" + largest;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
int k = scan.nextInt();
scan.close();
System.out.println(getSmallestAndLargest(s, k));
}
}