-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ17.java
More file actions
42 lines (38 loc) · 1.17 KB
/
Q17.java
File metadata and controls
42 lines (38 loc) · 1.17 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
37
38
39
40
41
42
// Java Substring Comparisionss
import java.util.Scanner;
public class Q17
{
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
// Complete the function
// 'smallest' must be the lexicographically smallest substring of length 'k'
// 'largest' must be the lexicographically largest substring of length 'k'
smallest = "z";
largest = "a";
if(k == s.length())
{
smallest = s;
largest = s;
}
else
{
for(int i = 0; i <= s.length() - k; i++) {
String substr = s.substring(i, i + k);
if(substr.compareTo(smallest) < 0)
smallest = substr;
if(substr.compareTo(largest) > 0)
largest = substr;
}
}
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));
}
}