-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.java
77 lines (53 loc) · 1.85 KB
/
day24.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Given a string, , and an integer, , complete the function so that it finds the lexicographically smallest and largest substrings of length .
Function Description
Complete the getSmallestAndLargest function in the editor below.
getSmallestAndLargest has the following parameters:
string s: a string
int k: the length of the substrings to find
Returns
string: the string ' + "\n" + ' where and are the two substrings
Input Format
The first line contains a string denoting .
The second line contains an integer denoting .
Constraints
consists of English alphabetic letters only (i.e., [a-zA-Z]).
Sample Input 0
welcometojava
3
Sample Output 0
ava
wel
Solution -
*/
import java.util.Scanner;
public class Solution {
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'
java.util.ArrayList<String> list = new java.util.ArrayList<>();
for(int i = 0; i <= s.length() - k; i++){
//System.out.println(s.substring(i, i+k));
list.add(s.substring(i, i+k));
}
smallest = list.get(0);
largest = list.get(0);
for(String i : list){
if(i.compareTo(smallest) < 0)
smallest = i;
else if(i.compareTo(largest) > 0)
largest = 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));
}
}