-
Notifications
You must be signed in to change notification settings - Fork 5
/
1239. Maximum Length of a Concatenated String with Unique Characters.java
64 lines (64 loc) · 2.2 KB
/
1239. Maximum Length of a Concatenated String with Unique Characters.java
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
class Solution {
private int globalMaxlength = 0;
public int maxLength(List<String> arr) {
if (arr.size() == 0 || arr == null)
return 0;
this._maxlength(arr, "", 0);
return globalMaxlength;
}
private int _maxlength(List<String> arr, String asf, int curindex) {
boolean isUniq = this.isAllCharactersUniq(asf, "");
if (!isUniq) {
return 0;
}
globalMaxlength = Math.max(globalMaxlength, asf.length());
for (int i = curindex; i < arr.size(); i++) {
String t = arr.get(i);
this._maxlength(arr, asf + t, i + 1);
}
return globalMaxlength;
}
public int maxLength2(List<String> arr) {
if (arr.size() == 1) {
if (isAllCharactersUniq(arr.get(0), "")) {
return arr.get(0).length();
}
}
int globalMaxlength = 0;
for (int i = 0; i < arr.size(); i++) {
StringBuilder sb = new StringBuilder(arr.get(i));
if (isAllCharactersUniq(sb.toString(), "")) {
globalMaxlength = Math.max(globalMaxlength, sb.length());
}
for (int j = i + 1; j < arr.size(); j++) {
String candidate = arr.get(j);
if (isAllCharactersUniq(sb.toString(), candidate)) {
sb.append(candidate);
System.out.println(sb.toString());
globalMaxlength = Math.max(globalMaxlength, sb.length());
}
}
}
return globalMaxlength;
}
private boolean isAllCharactersUniq(String s, String candidate) {
Set<Character> set = new HashSet<>();
for (char c : s.toCharArray())
set.add(c);
for (char c : candidate.toCharArray())
set.add(c);
int finalStringLength = s.length() + candidate.length();
return set.size() == finalStringLength;
}
}