-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
53 lines (43 loc) · 1.3 KB
/
Solution.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
// https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters
class Solution {
public int maxLength(List<String> arr) {
int n = arr.size();
List<String> all_pos = new ArrayList<>();
for(String str: arr){
List<String> temp = new ArrayList<>();
for(String s: all_pos){
if(canCombine(str, s)){
temp.add(str + s);
}
}
for(String s: temp){
all_pos.add(s);
}
if(canCombine(str, "")){
all_pos.add(str);
}
}
int len = 0;
for(String str: all_pos){
len = Math.max(len, str.length());
}
return len;
}
public boolean canCombine(String s1, String s2){
int f[] = new int[26];
for(int i=0;i<s1.length();i++){
char ch = s1.charAt(i);
f[ch - 'a']++;
if(f[ch - 'a'] > 1){
return false;
}
}
for(int i=0;i<s2.length();i++){
char ch = s2.charAt(i);
if(f[ch - 'a'] > 0){
return false;
}
}
return true;
}
}