-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution1239.java
57 lines (48 loc) · 1.51 KB
/
Solution1239.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
import java.util.ArrayList;
import java.util.List;
/**
* Created by slade on 2019/12/26.
*/
public class Solution1239 {
public int maxLength(List<String> arr) {
return dfs(0, "", arr);
}
public int dfs(int idx, String tmp, List<String> arr) {
if (idx >= arr.size()) {
return tmp.length();
}
String s = arr.get(idx);
if (isNotDuplicate(s) && !isCross(tmp, s)) {
return Math.max(dfs(idx + 1, tmp + s, arr), dfs(idx + 1, tmp, arr));
} else {
return dfs(idx + 1, tmp, arr);
}
}
public Boolean isNotDuplicate(String s) {
for (int i = 0; i < s.length(); i++) {
char charWord = s.charAt(i);
int fristPosition = s.indexOf(charWord);
int lastPosition = s.lastIndexOf(charWord);
if (fristPosition != lastPosition) {
return false;
}
}
return true;
}
public Boolean isCross(String s1, String s2) {
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
if (s1.charAt(i) == s2.charAt(j)) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
Solution1239 solution1239 = new Solution1239();
ArrayList arrayList = new ArrayList();
arrayList.add("abcdefghijklmnopqrstuvwxyz");
System.out.println(solution1239.maxLength(arrayList));
}
}