-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJavaSubstringComparison.java
More file actions
27 lines (21 loc) Β· 923 Bytes
/
JavaSubstringComparison.java
File metadata and controls
27 lines (21 loc) Β· 923 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
// https://www.hackerrank.com/challenges/java-string-compare/problem
import java.util.Scanner;
public class JavaSubstringComparison {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
int k = scanner.nextInt();
scanner.close();
System.out.println(getSmallestAndLargest(string, k));
}
private static String getSmallestAndLargest(String string, int k) {
String largest;
String smallest = largest = string.substring(0, k);
for (int index = 1 ; index < string.length() - k + 1 ; index++) {
String subString = string.substring(index, index + k);
smallest = smallest.compareTo(subString) < 0 ? smallest : subString;
largest = largest.compareTo(subString) < 0 ? subString : largest;
}
return smallest + "\n" + largest;
}
}