forked from builder-of-web3/HackR_Java_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava String Compare
More file actions
71 lines (55 loc) · 1.77 KB
/
Java String Compare
File metadata and controls
71 lines (55 loc) · 1.77 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
Given a string, find out the lexicographically smallest and largest substring of length k.
[Note: Lexicographic order is also known as alphabetic order dictionary order. So "ball" is smaller than "cat", "dog" is smaller than "dorm". Capital letter always comes before smaller letter, so "Happy" is smaller than "happy" and "Zoo" is smaller than "ball".]
Input Format
First line will consist a string containing english alphabets which has at most 1000 characters. 2nd line will consist an integer k.
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
Among them ava is the smallest and wel is the largest.
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 sc = new Scanner(System.in);
String str = sc.nextLine();
int n = sc.nextInt();
String max = str.substring(0,n);
for(int i = 0; i<=str.length()-n;i++){
String temp = str.substring(i,i+n);
if(temp.compareTo(max) >= 0){
max = temp;
}
}
String min = str.substring(0,n);
for(int i = 0; i<=str.length()-n;i++){
String temp = str.substring(i,i+n);
if(temp.compareTo(max) < 0){
min = temp;
}
}
System.out.println(min);
System.out.println(max);
}
}