-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestUncommonSubstring.java
44 lines (42 loc) · 1.34 KB
/
LongestUncommonSubstring.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
import java.util.HashSet;
public class LongestUncommonSubstring {
public int findLUSlength(String[] strs) {
String subsequence = strs[0];
int subIndex = 0;
HashSet<String> badStrings = new HashSet<String>();
for (int i = 1; i < strs.length; i++) {
System.out.println(subsequence);
// current sequence is present in another String
if (!subsequence.equals("") && strs[i].contains(subsequence)) {
// current sequence is a subsequence
if (subsequence.length() < strs[i].length()) {
badStrings.add(subsequence);
if (!badStrings.contains(strs[i])) {
subsequence = strs[i];
subIndex = i;
i = 0;
} else {
subsequence = "";
}
// current sequence is equivalent to another String
} else {
if (subIndex != i) {
badStrings.add(subsequence);
subsequence = "";
i = 0;
}
}
// if current sequence is not in this String but is smaller
} else if (strs[i].length() > subsequence.length() && !badStrings.contains(strs[i])) {
subsequence = strs[i];
subIndex = i;
i = 0;
}
}
System.out.println("final " + subsequence);
if (subsequence.length() == 0) {
return -1;
}
return subsequence.length();
}
}