Repository navigation
Expand file tree
/
Copy pathLeetCode748.java
More file actions
59 lines (54 loc) · 2.52 KB
/
Copy pathLeetCode748.java
File metadata and controls
59 lines (54 loc) · 2.52 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
//748. Shortest Completing Word
public class LeetCode748 {
//Runtime: 46ms
public String shortestCompletingWord(String licensePlate, String[] words) {
Map<Integer, Integer> patternMap = licensePlate.chars()
.boxed()
.filter(Character::isAlphabetic)
.map(Character::toLowerCase)
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
var minString = Arrays.stream(words)
.filter(target -> {
var targetMap = target.chars()
.boxed()
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
return patternMap.entrySet().stream()
.allMatch(entry -> targetMap.getOrDefault(entry.getKey(), 0) >= entry.getValue());
})
.collect(Collectors.groupingBy(String::length))
.entrySet()
.stream()
.min((e1, e2) -> Integer.compare(e1.getKey(), e2.getKey()));
return minString.isPresent() ? minString.get().getValue().getFirst() : "";
}
public String shortestCompletingWord_2(String licensePlate, String[] words) {
Map<Integer, Integer> patternMap = licensePlate.chars()
.boxed()
.filter(Character::isAlphabetic)
.map(Character::toLowerCase)
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
var minStringMap = Arrays.stream(words)
.filter(target -> {
var targetMap = target.chars()
.boxed()
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
return patternMap.entrySet().stream()
.allMatch(entry -> targetMap.getOrDefault(entry.getKey(), 0) >= entry.getValue());
})
.collect(Collectors.toMap(
String::length,
str -> new ArrayList<String>(){{add(str);}},
(ex, cu) -> {
ex.addAll(cu); // Combine the lists
return ex; // Return the combined list
},
TreeMap::new
));
return !minStringMap.isEmpty() ? minStringMap.firstEntry().getValue().getFirst() : "";
}
}