Repository navigation
Expand file tree
/
Copy pathLeetCode1239.java
More file actions
117 lines (107 loc) · 3.5 KB
/
Copy pathLeetCode1239.java
File metadata and controls
117 lines (107 loc) · 3.5 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.yeahqing.medium._1239;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* @FileName LeetCode1239.java
* @Desc java-leetcode created by YeahQing
* @Author YeahQing
* @Date 2022/10/27 14:14
*/
public class LeetCode1239 {
public static void main(String[] args) {
Solution solution = new Solution2();
List<String> testCase = new ArrayList<>(Arrays.asList("un", "iq", "ue"));
System.out.println(solution.maxLength(testCase));
}
}
class Solution {
int ans = 0;
public int maxLength(List<String> arr) {
List<Integer> masks = new ArrayList<Integer>();
for (String s : arr) {
Integer mask = 0;
for (Character c : s.toCharArray()) {
int ch = c - 'a';
if ((mask & 1 << ch) != 0) {
mask = 0;
break;
}
mask = mask | 1 << ch;
}
if (mask > 0) {
masks.add(mask);
}
}
dfs(masks, 0, 0);
return ans;
}
public void dfs(List<Integer> masks, int pos, int mask) {
if (pos == masks.size()) {
ans = Math.max(ans, Integer.bitCount(mask));
return;
}
if ((mask & masks.get(pos)) == 0) {
dfs(masks, pos + 1, mask | masks.get(pos));
}
dfs(masks, pos + 1, mask);
}
}
class Solution1 extends Solution {
@Override
public int maxLength(List<String> arr) {
// 使用哈希集合去重
List<HashSet<Character>> all = new ArrayList<>();
all.add(new HashSet<>());
int ans = 0;
for (String str : arr) {
for (int i = 0; i < all.size(); i++) {
HashSet<Character> s = all.get(i);
// 用一个哈希集合中的元素去构建新的哈希集合
HashSet<Character> tmp = new HashSet<>(all.get(i));
for (Character c : str.toCharArray()) {
tmp.add(c);
}
// 当前集合中的字符串和当前字符串没有重复
if (tmp.size() == s.size() + str.length()) {
all.add(tmp);
ans = Math.max(ans, tmp.size());
}
}
}
return ans;
}
}
class Solution2 extends Solution {
@Override
public int maxLength(List<String> arr) {
int ans = 0;
List<Integer> masks = new ArrayList<>();
masks.add(0); // 空串
for (String s : arr) {
// 1. 判断字符串中有没有重复的字符
int mask = 0;
for (Character c : s.toCharArray()) {
int ch = c - 'a';
if ((mask & 1 << ch) != 0) {
mask = 0;
break;
}
// 没有重复字符,当前字符串就是可行解的一种
mask |= 1 << ch;
}
// 有重复字符,直接跳过
if (mask == 0) continue;
// 2. 和前i项可行解做拼接,判断是否满足条件
for (int i = 0; i < masks.size(); i++) {
if ((mask & masks.get(i)) == 0) {
// 和空串拼接,就是将当前串添加到可行解中
masks.add(mask | masks.get(i));
ans = Math.max(ans, Integer.bitCount(mask | masks.get(i)));
}
}
}
return ans;
}
}