-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJava Substring Comparisons
More file actions
40 lines (30 loc) · 984 Bytes
/
Java Substring Comparisons
File metadata and controls
40 lines (30 loc) · 984 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
38
39
40
import java.util.Scanner;
public class Solution {
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
String[] list = new String[s.length() - k + 1];
for (int i = 0; i <= s.length() - k; i++) {
String str = s.substring(i, i+k);
list[i] = str;
}
smallest = list[0];
largest = list[0];
for(int i = 1; i < list.length; i++) {
if (list[i].compareTo(smallest) < 0) {
smallest = list[i];
}
if (list[i].compareTo(largest) > 0){
largest = list[i];
}
}
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));
}
}