Repository navigation
Expand file tree
/
Copy pathLeetCode1897.java
More file actions
55 lines (42 loc) · 1.52 KB
/
Copy pathLeetCode1897.java
File metadata and controls
55 lines (42 loc) · 1.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
import org.w3c.dom.ls.LSOutput;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Collectors;
public class LeetCode1897 {
// Redistribute Characters to Make All Strings Equal
public boolean makeEqual1(String[] words) {
int[] freq = new int['z' + 1];
for (String s : words) {
for (char ch : s.toCharArray()) {
freq[ch]++;
}
}
for (int i = 'a'; i <= 'z'; i++) {
if (freq[i] % words.length != 0)
return false;
}
return true;
}
public boolean makeEqual2(String[] words) {
return Arrays.stream(words).flatMap( n -> n.chars().mapToObj(c -> (char)c) ).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.allMatch( entry -> entry.getValue() % words.length ==0);
// return false;
}
public boolean makeEqual(String[] words) {
return Arrays.stream(words)
.flatMapToInt(String::chars)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.values()
.stream()
.allMatch(count -> count % words.length == 0);
}
public static void main(String[] args) {
LeetCode1897 obj = new LeetCode1897();
String[] words = {"abc", "aabc", "bc"};
System.out.println(obj.makeEqual(words));
}
}