Challenge: Long Words
Completed
Read a line of words and a minimum length k on the next line. Using a stream, print the distinct words (in lower case) that have at least k letters, sorted alphabetically and joined by commas and spaces. If there are none, print none.
Input
Line 1: words separated by single spaces. Line 2: a whole number k.
Output
For example banana, cherry.
Example
Input
Cherry apple Banana fig cherry 6
Output
banana, cherry
Editor
Java
Hints
Arrays.stream(array) makes a stream from an array.
Use map, filter, distinct, sorted, then Collectors.joining(", ").
Solution explanation
Lower-casing first makes Cherry and cherry the same word, so distinct() removes the copy.
Show solution
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] words = sc.nextLine().trim().split(" ");
int k = sc.nextInt();
String result = Arrays.stream(words)
.map(String::toLowerCase)
.filter(w -> w.length() >= k)
.distinct()
.sorted()
.collect(Collectors.joining(", "));
System.out.println(result.isEmpty() ? "none" : result);
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module