-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThirtySeven.java
More file actions
37 lines (29 loc) · 1004 Bytes
/
ThirtySeven.java
File metadata and controls
37 lines (29 loc) · 1004 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
27
28
29
30
31
32
33
34
35
36
37
package HackerRank;
import java.util.Scanner;
public class ThirtySeven {
public static String getSmallestAndLargest(String s, int k) {
String smallest = s.substring(0, k);
String largest = s.substring(0, k);
// Complete the function
// 'smallest' must be the lexicographically smallest substring of length 'k'
// 'largest' must be the lexicographically largest substring of length 'k'
for (int i = 1; i <= s.length() - k; i++) {
String substring = s.substring(i, i + k);
if (substring.compareTo(smallest) < 0) {
smallest = substring;
}
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));
}
}
}