58.7k views
2 votes
We define the following terms:

Lexicographical Order, also known as alphabetic or dictionary order, orders characters as follows:
For example, ball < cat, dog < dorm, Happy < happy, Zoo < ball.
A substring of a string is a contiguous block of characters in the string. For example, the substrings of abc are a, b, c, ab, bc, and abc.
Given a string, , and an integer, , complete the function so that it finds the lexicographically smallest and largest substrings of length .
Input Format
The first line contains a string denoting .
The second line contains an integer denoting .
Constraints
consists of English alphabetic letters only (i.e., [a-zA-Z]).
Output Format
Return the respective lexicographically smallest and largest substrings as a single newline-separated string.
Sample Input 0
welcometojava
3
Sample Output 0
ava
wel
Explanation 0
String has the following lexicographically-ordered substrings of length :
We then return the first (lexicographically smallest) substring and the last (lexicographically largest) substring as two newline-separated values (i.e., ava\\wel).
The stub code in the editor then prints ava as our first line of output and wel as our second line of output.
Solution:-
import java.util.Scanner;
public class Solution {
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
smallest = largest = s.substring(0, k);
for (int i=1; i String substr = s.substring(i, i+k);
if (smallest.compareTo(substr) > 0)
smallest = substr;
if (largest.compareTo(substr) < 0)
largest = substr;
}
return smallest + "\\" + largest;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
int k = scan.nextInt();
scan.close();
System.out.println(getSmallestAndLargest(s, k));
}
}

User Bwagner
by
5.7k points

1 Answer

3 votes

Answer:

Here is the JAVA program:

import java.util.*;

public class Solution { // class name

public static String getSmallestAndLargest(String s, int k) { //method that takes a string s and and integer k and returns lexicographically smallest and largest substrings

String smallest = ""; //stores the smallest substring

String largest = ""; //stores the largest substring

smallest = largest = s.substring(0, k); //sets the smallest and largest to substring from 0-th start index and k-th end index of string s

for(int i = 0;i<=s.length()-k;i++){ //iterates through the string s till length()-k

String subString = s.substring(i,i+k); //stores the substring of string s from ith index to i+k th index

if(i == 0){ //if i is equal to 0

smallest = subString; } //assigns subString to smallest

if(subString.compareTo(largest)>0){ //checks if the subString is lexicographically greater than largest

largest = subString; //sets subString to largest

}else if(subString.compareTo(smallest)<0) //checks if the subString is lexicographically less than smallest

smallest = subString; } //sets subString to smallest

return smallest + "\\" + largest; } //returns the lexicographically smallest and largest substrings

public static void main(String[] args) { //start of main method

Scanner scan = new Scanner(System.in); //creates Scanner object

String s = scan.next(); //scans and reads input string from user

int k = scan.nextInt(); //scans and reads integer k from user

scan.close();

System.out.println(getSmallestAndLargest(s, k)); } } //calls method by passing string s and integer k to it to display lexicographically smallest and lexicographically largest substring

Step-by-step explanation:

The program takes a string s and an integer k and passes them to function getSmallestAndLargest that returns the lexicographically smallest and lexicographically largest substring. The method works as follows:

Lets say s = helloworld and k = 3

smallest = largest = s.substring(0, k);

s.substring(0, k); is returns the substring from 0th index to k-th index so it gives substring hel

for(int i = 0;i<=s.length()-k;i++) This loop iterates through the string s till s.length()-k times

s.length()-k is equal to 7 in this example

At first iteration:

i = 0

i<=s.length()-k is true so program enters the body of loop

String subString = s.substring(i,i+k); this becomes:

subString = s.substring(0,3);

subString = "hel"

if(i == 0) this is true so:

smallest = subString;

smallest = "hel"

At second iteration:

i = 1

i<=s.length()-k is true so program enters the body of loop

String subString = s.substring(i,i+k); this becomes:

subString = s.substring(1,4);

subString = "ell"

if(subString.compareTo(smallest)<0) this condition is true because the subString ell is compared to smallest hel and it is lexographically less than smallest so :

smallest = subString;

smallest = "ell"

So at each iteration 3 characters of string s are taken and if and else if condition checks if these characters are lexicographically equal, smaller or larger and the values of largest and smallest change accordingly

After the loop ends the statement return smallest + "\\" + largest; executes which returns the smallest and largest substrings. So the output of the entire program with given example is:

ell wor

Here ell is the lexicographically smallest substring and wor is lexicographically largest substring in the string helloworld

The screenshot of the program along with its output is attached.

We define the following terms: Lexicographical Order, also known as alphabetic or-example-1
User Caspian Canuck
by
5.8k points