-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode1239.java
178 lines (145 loc) · 4.64 KB
/
LeetCode1239.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package techQuestions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class LeetCode1239 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// String[] test = {"un","iq","ue"};
//
// System.out.println(arr.length);
// System.out.println(arr[0]);
// String sTest = arr[0];
// System.out.println(s.length());
List<String> arr = new ArrayList<String>();
arr.add("cha");
arr.add("r");
arr.add("act");
arr.add("ers");
System.out.println(lastTry(arr));
}
public static int maxLength(List<String> arr) {
List<Integer> dp = new ArrayList<>();
dp.add(0);
int res = 0;
for (String s : arr) {
System.out.print(s + ": ");
int a = 0, dup = 0;
for (char c : s.toCharArray()) {
int ASCII = c - 'a';
System.out.println("current char is: " + c + ", and ASCII is: " + ASCII);
dup |= a & (1 << (c - 'a'));
a |= 1 << (c - 'a');
System.out.print("dup: " + dup + ", a_value: " + a + " || ");
}
if (dup > 0) continue;
System.out.println("dp size is: " + dp.size());
for (int i = dp.size() - 1; i >= 0; --i) {
if ((dp.get(i) & a) > 0) continue;
System.out.println("dp.get(" + i + ") | " + a + " = " + (dp.get(i) | a));
dp.add(dp.get(i) | a);
res = Math.max(res, Integer.bitCount(dp.get(i) | a));
}
System.out.println();
}
return res;
}
public static int myMethod(List<String> arr) {
int[] alphabetCount = new int[26];
List<String> verifiedSubstring = new ArrayList<>();
ArrayList<int[]> characterCounts = new ArrayList<int[]>();
for (String s : arr) {
boolean omitSubstring = false;
alphabetCount = new int[26];
for (char c : s.toCharArray()) {
int ASCII = c - 'a';
alphabetCount[ASCII] += 1;
if (alphabetCount[ASCII] > 1) {
omitSubstring = true;
System.out.println("you will exit this loop early");
break;
}
}
System.out.println(Arrays.toString(alphabetCount));
if (omitSubstring == true) {
System.out.println("time to move on");
continue;
}
System.out.println(s + " is verified");
characterCounts.add(alphabetCount);
verifiedSubstring.add(s);
}
System.out.println(verifiedSubstring.toString());
int j = 0;
for (int[] currentAlphaCount : characterCounts) {
for (int i = j + 1; i < characterCounts.size(); i++) {
}
j++;
System.out.println(currentAlphaCount[4]);
}
return 9999;
}
public static boolean isUnique(String s) {
if (s.length() > 26) {
return false;
}
boolean[] appeared = new boolean[26];
for (char c : s.toCharArray()) {
if (appeared[(c-'a')]) {
return false;
}
appeared[(c-'a')] = true;
}
return true;
}
public static int lastTry(List<String> arr) {
List<String> res = new ArrayList<>();
// initialize res with an empty string, therefore we can compare a string s with itself
res.add("");
for (String s : arr) {
// if this substring is not unique itself, stop testing on it
if (!isUnique(s)) {
continue;
}
// this list of strings will hold all valid concatenations of s and res strings
List<String> resList = new ArrayList<>();
// combining current "s" string and all strings in res list thus far
for (String candidate : res) {
String temp = candidate + s;
// System.out.println(temp);
if (isUnique(temp)) {
resList.add(temp);
}
}
// System.out.println(resList.toString());
// add all the unique strings to resList
res.addAll(resList);
}
int maxLength = 0;
for (String concat : res) {
maxLength = Math.max(concat.length(), maxLength);
}
return maxLength;
}
// Iterator<String> strings = arr.iterator();
//
// int[] alphabetCount = new int[26];
// while (strings.hasNext()) {
// System.out.println(strings.next());
// }
// for (int i = 0; i<arr.size(); i++) {
// String s = arr.get(i);
// for (int j = 0; j<s.length(); j++) {
// int alphaIndex = s.charAt(j) - 'a';
// alphabetCount[alphaIndex]++;
// }
// for (int z = 0; z<alphabetCount.length; z++) {
// System.out.print(alphabetCount[z] + " ");
// }
// alphabetCount = new int[26];
// System.out.println();
// }
//
// return 0;
}