-
Notifications
You must be signed in to change notification settings - Fork 0
/
14- Java Substring Comparisons.java
42 lines (31 loc) · 1.09 KB
/
14- Java Substring Comparisons.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
/*
problem: https://www.hackerrank.com/challenges/java-string-compare/problem
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
// get the inputs
Scanner sc = new Scanner(System.in);
String s = sc.next();
int k = sc.nextInt();
String smallest="", largest="";
int size=0;
// split the substrings and add it to the list
ArrayList<String> mList = new ArrayList<String>();
for(int i=0; i<s.length()-(k-1); i++)
{
mList.add(s.substring(i,i+k));
size++;
}
// convert the list to an array
String[] mArray= new String[size];
mArray = mList.toArray(mArray);
// sort the array
Arrays.sort(mArray);
// output the results
System.out.println(mArray[0]);
System.out.println(mArray[size-1]);
}
}