|
2 | 2 |
|
3 | 3 | importjava.util.ArrayList;
|
4 | 4 | importjava.util.Arrays;
|
| 5 | +importjava.util.HashSet; |
5 | 6 | importjava.util.List;
|
6 | 7 |
|
7 | 8 | /**
|
|
13 | 14 |
|
14 | 15 | publicclassLeetCode1239 {
|
15 | 16 | publicstaticvoidmain(String[]args) {
|
16 |
| -Solutionsolution =newSolution(); |
17 |
| -List<String>testCase =newArrayList<>(Arrays.asList("aa","bb")); |
| 17 | +Solutionsolution =newSolution1(); |
| 18 | +List<String>testCase =newArrayList<>(Arrays.asList("un","iq","ue")); |
18 | 19 | System.out.println(solution.maxLength(testCase));
|
19 | 20 | }
|
20 | 21 | }
|
@@ -54,3 +55,28 @@ public void dfs(List<Integer> masks, int pos, int mask) {
|
54 | 55 | dfs(masks,pos +1,mask);
|
55 | 56 | }
|
56 | 57 | }
|
| 58 | + |
| 59 | +classSolution1extendsSolution { |
| 60 | +@Override |
| 61 | +publicintmaxLength(List<String>arr) { |
| 62 | +List<HashSet<Character>>all =newArrayList<>(); |
| 63 | +all.add(newHashSet<>()); |
| 64 | +intans =0; |
| 65 | +for (Stringstr :arr) { |
| 66 | +for (inti =0;i <all.size();i++) { |
| 67 | +HashSet<Character>s =all.get(i); |
| 68 | +// 用一个哈希集合中的元素去构建新的哈希集合 |
| 69 | +HashSet<Character>tmp =newHashSet<>(all.get(i)); |
| 70 | +for (Characterc :str.toCharArray()) { |
| 71 | +tmp.add(c); |
| 72 | + } |
| 73 | +// 当前集合中的字符串和当前字符串没有重复 |
| 74 | +if (tmp.size() ==s.size() +str.length()) { |
| 75 | +all.add(tmp); |
| 76 | +ans =Math.max(ans,tmp.size()); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +returnans; |
| 81 | + } |
| 82 | +} |