-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSolution.java
More file actions
26 lines (20 loc) · 736 Bytes
/
Copy pathSolution.java
File metadata and controls
26 lines (20 loc) · 736 Bytes
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
import java.util.Scanner;
public class Solution {
public static String getSmallestAndLargest(String s, int k) {
String smallest = s.substring(0, k);
String largest = s.substring(0, k);
for (int i = 1; i <= s.length() - k; i++) {
String x = s.substring(i, i + k);
if (smallest.compareTo(x) > 0) smallest = x;
if (largest.compareTo(x) < 0) largest = x;
}
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));
}
}