-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstringCompare.java
More file actions
38 lines (31 loc) · 987 Bytes
/
SubstringCompare.java
File metadata and controls
38 lines (31 loc) · 987 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
33
34
35
36
37
38
import java.util.Scanner;
public class Solution {
//Start Code
/*
https://www.hackerrank.com/challenges/java-string-compare
*/
public static String getSmallestAndLargest(String str, int k) {
int n = str.length()-k+1;
String[] subs = new String[n];
for(int i=0;i<n;i++)
subs[i] = str.substring(i,i+k);
String temp;
String smallest = subs[0];
String largest = subs[0];
for(int i=0;i<n;i++){
if(subs[i].compareTo(smallest)<0)
smallest = subs[i];
if(subs[i].compareTo(largest)>0)
largest = subs[i];
}
return smallest + "\n" + largest;
}
// End Code
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));
}
}