Skip to content

Commit 360ee46

Browse files
committed
Find the lexicographically smallest and largest substring
1 parent 2638b1c commit 360ee46

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Java/Strings/Compare.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Given a string, find out the lexicographically smallest and largest substring of length k.
3+
4+
[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".]
5+
6+
Input Format
7+
First line will consist a string containing english alphabets which has at most 1000 characters. 2nd line will consist an integer k.
8+
9+
Output Format
10+
In the first line print the lexicographically minimum substring. In the second line print the lexicographically maximum substring.
11+
*/
12+
13+
import java.io.*;
14+
import java.util.*;
15+
16+
public class Compare {
17+
18+
public static void main(String[] args) {
19+
Scanner sc=new Scanner(System.in);
20+
String A=sc.next();
21+
int B=sc.nextInt();
22+
ArrayList<String> ar = new ArrayList<String>();
23+
for (int i = 0; i <= A.length()-B; i++) {
24+
String a = A.substring(i, i+B);
25+
ar.add(a);
26+
}
27+
ar.sort(null);
28+
System.out.println(ar.get(0));
29+
System.out.println(ar.get(ar.size()-1));
30+
}
31+
}

0 commit comments

Comments
 (0)