subject

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\nwel).
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 + "\n" + 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));
}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 07:00
For all machines-not just hammers- the user applies force force to the machine to the machine over a certain distance. a. input b. output c. duo d. none of the above
Answers: 1
question
Computers and Technology, 22.06.2019 11:30
Awell-diversified portfolio needs about 20-25 stocks from different categories is this true or false?
Answers: 2
question
Computers and Technology, 23.06.2019 02:00
Which demographic challenge is europe currently experiencing? a. an aging and decreasing population b. a baby boomc. an unequal distribution between males and females d. a large group of teenagers moving through the school system(i chose a but i'm unsure)
Answers: 1
question
Computers and Technology, 23.06.2019 04:00
In a word processing program, such as microsoft word, which feature to you choose the desired picture enhancement?
Answers: 2
You know the right answer?
We define the following terms: Lexicographical Order, also known as alphabetic or dictionary order,...
Questions
question
Mathematics, 21.04.2021 22:20
question
Mathematics, 21.04.2021 22:30
question
Physics, 21.04.2021 22:30
question
Computers and Technology, 21.04.2021 22:30
question
Mathematics, 21.04.2021 22:30
Questions on the website: 13722362