-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
36 lines (32 loc) · 1.39 KB
/
Copy pathSolution.java
File metadata and controls
36 lines (32 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.stream.IntStream;
class Solution
{ /*Counts the number of occurrences of alphabets in a string*/
private static BiConsumer<String, int[]> COUNTER =
(w, counts) -> w.chars().mapToObj(i -> (char) i).filter(Character::isAlphabetic).map(Character::toLowerCase).forEach(c -> counts[c - 'a']++); /*Checks if the counts of alphabets in a word are suffice to construct the license plate string*/
private static BiPredicate<int[], int[]> MATCHER =
(counts, charCounts) -> IntStream.range(0, 26).noneMatch(i -> counts[i] > charCounts[i]);
public String shortestCompletingWord(String licensePlate, String[] words)
{
int[] counts = new int[26];
COUNTER.accept(licensePlate, counts);
String shortestWord = null;
int shortestLen = Integer.MAX_VALUE;
for (String word : words)
{ /*Consider only first occurrence of the shortest string*/
if (word.length() >= shortestLen)
{
continue;
}
int[] charCounts = new int[26];
COUNTER.accept(word, charCounts);
if (MATCHER.test(counts, charCounts))
{
shortestLen = word.length();
shortestWord = word;
}
}
return shortestWord;
}
}