-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
61 lines (57 loc) · 1.48 KB
/
Copy pathSolution.java
File metadata and controls
61 lines (57 loc) · 1.48 KB
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
import java.util.Arrays;
import java.util.Comparator;
public class Solution
{
public int findLUSlength(String[] strs)
{
int count = -1;
Arrays.sort(strs, new Comparator<String>()
{
@Override
public int compare(String a, String b)
{
return a.length() - b.length();
}
});
for (int i = strs.length - 1; i >= 0; i--)
{
boolean isCommonSuqsequence = false;
for (int j = strs.length - 1; j >= 0; j--)
{
isCommonSuqsequence = i == j ? isCommonSuqsequence : isSubSequence(strs[i], strs[j]);
if (isCommonSuqsequence)
{
break;
}
}
if (!isCommonSuqsequence)
{
count = Math.max(count, strs[i].length());
}
}
return count;
}
private boolean isSubSequence(String subSequence, String b)
{
if (subSequence.length() > b.length())
{
return false;
}
if (subSequence.equals(b))
{
return true;
}
int prevIdx = -1;
for (int i = 0; i < subSequence.length(); i++)
{
char c = subSequence.charAt(i);
int idx = b.indexOf(c, prevIdx + 1);
if (idx == -1)
{
return false;
}
prevIdx = idx;
}
return true;
}
}