forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
78 lines (65 loc) · 1.74 KB
/
Solution.java
File metadata and controls
78 lines (65 loc) · 1.74 KB
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
78
package strings;
//Given a string, find out the lexicographically smallest and largest substring of length .
//
//Input Format
//
//First line will consist a string containing english alphabets which has at most characters. 2nd line will consist an integer .
//
//Output Format
//
//In the first line print the lexicographically minimum substring.
//In the second line print the lexicographically maximum substring.
//Sample Input
//
//welcometojava
//3
//Sample Output
//
//ava
//wel
//Explanation
//Here is the list of all substrings of length 3:
//
//wel
//elc
//lco
//com
//ome
//met
//eto
//toj
//oja
//jav
//ava
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
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. */
Scanner input = new Scanner(System.in);
String s;
int subLength;
s=input.nextLine();
subLength=Integer.parseInt(input.nextLine());
input.close();
int iterations = s.length()-subLength;
String largest = s.substring(0,subLength);
String smallest = s.substring(0,subLength);
for(int i = 0; i<=iterations;i++)
{
if(largest.compareTo(s.substring(i,i+subLength))<0)
{
largest = s.substring(i,i+subLength);
}
if(smallest.compareTo(s.substring(i,i+subLength))>0)
{
smallest = s.substring(i,i+subLength);
}
}
System.out.println(smallest);
System.out.println(largest);
}
}