-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java Substring Comparisons
44 lines (35 loc) · 1 KB
/
Java Substring Comparisons
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
41
42
43
44
/*We define the following terms:
Lexicographical Order, also known as alphabetic or dictionary order, orders characters as follows:
Sample Input 0
welcometojava
3
Sample Output 0
ava
wel
code
import java.util.Scanner;
public class Solution {
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
smallest = s.substring(0,k);
largest = s.substring(0,k);
for(int i=0; i<=s.length()-k; i++ ){
String str = s.substring(i,k+i);
if (smallest.compareTo(str)>0){
smallest = str;
}
if(largest.compareTo(str)<0){
largest=str;
}
}
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));
}
}