We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
try(Scanner s = new Scanner(System.in)){
//System.out.println("Enter your string");
String str = s.next();
//System.out.println("Enter Sub String size or length");
int len = s.nextInt();
System.out.println(getSmallestAndLargest(str, len));
}
}
public static String getSmallestAndLargest(final String str, final int l){
if(Objects.isNull(str) || str.isBlank() || str.length()< l || l<=0 || str.length()>1000){
return "";
}
List<String> s = IntStream.range(0, str.length()-l+1)
.mapToObj(i-> str.substring(i, i+l))
.map(String::trim)
.distinct()
.sorted()
.collect(Collectors.toList());
if(s.size() >2){
return String.format("%s"+"\n"+"%s", s.get(0), s.get(s.size()-1));
}
if(s.size() ==1){
return String.format("%s"+"\n"+"%s", s.get(0), s.get(0));
}
return "";
}
Java Substring Comparisons
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.util.stream.*;
public class Solution {
}
Why are you making it so complex when it can be done so simply and cleanly.