The below code snippet matches ONLY A FEW test cases | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The below code snippet matches ONLY A FEW test cases

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class substring { public static String getSmallestAndLargest(String s, int k) { String smallest = ""; String sub=""; String largest = ""; for(int i=0;i<=(s.length()-k);i++) { sub=s.substring(i,i+k); if(i==0) smallest=sub; if(sub.compareToIgnoreCase(largest) > 0) largest = sub ; if(sub.compareToIgnoreCase(smallest)<0) smallest=sub; } 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)); } }

26th Jun 2018, 9:03 PM
Smriti Rastogi
Smriti Rastogi - avatar
2 Answers
0
Given a string,s , and an integer,k , complete the function so that it finds the lexicographically smallest and largest substrings of length . Input Format The first line contains a string denoting s. The second line contains an integer denoting k. Output Format Return the respective lexicographically smallest and largest substrings as a single newline-separated string. Sample Input welcometojava 3 Sample Output ava wel
26th Jun 2018, 9:06 PM
Smriti Rastogi
Smriti Rastogi - avatar
0
how is the below test case valid.....?? sample input welcomeToJava 3 expected output as by the online compilers for hackerearth Jav wel
26th Jun 2018, 9:11 PM
Smriti Rastogi
Smriti Rastogi - avatar