- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaSubstringComparisons.java
More file actions
Latest commit
36 lines (29 loc) · 1.02 KB
/
Copy pathJavaSubstringComparisons.java
File metadata and controls
36 lines (29 loc) · 1.02 KB
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
importjava.util.Scanner;
publicclassSolution {
publicstaticStringgetSmallestAndLargest(Strings, intk) {
Stringsmallest = "";
Stringlargest = "";
// Complete the function
// 'smallest' must be the lexicographically smallest substring of length 'k'
// 'largest' must be the lexicographically largest substring of length 'k'
smallest = s.substring(0,k);
largest = s.substring(0,k);
for(inti=0; i<=s.length()-k; i++ ){
Stringstr = s.substring(i,k+i);
if (smallest.compareTo(str)>0){
smallest = str;
}
if(largest.compareTo(str)<0){
largest=str;
}
}
returnsmallest + "\n" + largest;
}
publicstaticvoidmain(String[] args) {
Scannerscan = newScanner(System.in);
Strings = scan.next();
intk = scan.nextInt();
scan.close();
System.out.println(getSmallestAndLargest(s, k));
}
}