-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetSmallestAndLargest.java
More file actions
32 lines (26 loc) · 889 Bytes
/
GetSmallestAndLargest.java
File metadata and controls
32 lines (26 loc) · 889 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
package HackerRank.Strings;
import java.util.SortedSet;
import java.util.TreeSet;
public class GetSmallestAndLargest {
public String getSmallestAndLargest(String str, int k) {
String smallest = "";
String largest = "";
/*
// Another way to implement the function;
String[] strArr = new String[str.length()-(k-1)];
for (int i = 0; i<str.length()-k+1; i++) {
strArr[i] = str.substring(i, k+i);
}
Arrays.sort(strArr);
smallest = strArr[0];
largest = strArr[strArr.length-1];
*/
SortedSet<String> srdst = new TreeSet<String>();
for(int i=0;i<=str.length()-k;i++){
srdst.add(str.substring(i,i+k));
}
smallest = srdst.first();
largest = srdst.last();
return smallest + "/n" + largest;
}
}