|
4 | 4 | importjava.util.Comparator;
|
5 | 5 |
|
6 | 6 | /**
|
7 |
| - * Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. |
8 |
| -
|
9 |
| - A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. |
10 |
| -
|
11 |
| - The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. |
| 7 | + * 522. Longest Uncommon Subsequence II |
| 8 | + * |
| 9 | + * Given a list of strings, you need to find the longest uncommon subsequence among them. |
| 10 | + * The longest uncommon subsequence is defined as the longest subsequence of one of these strings and |
| 11 | + * this subsequence should not be any subsequence of the other strings. |
| 12 | + * A subsequence is a sequence that can be derived from one sequence by deleting some characters |
| 13 | + * without changing the order of the remaining elements. |
| 14 | + * Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. |
| 15 | + * The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. |
| 16 | + * If the longest uncommon subsequence doesn't exist, return -1. |
12 | 17 |
|
13 | 18 | Example 1:
|
14 | 19 | Input: "aba", "cdc", "eae"
|
|
20 | 25 | */
|
21 | 26 | publicclass_522 {
|
22 | 27 |
|
23 |
| -//Idea: if there's such a LUS there in the list, it must be one of the strings in the given list, |
24 |
| -//so we'll just go through the list and check if one string is NOT subsequence of any others, if so, return it, otherwise, return -1 |
25 |
| -publicintfindLUSlength(String[]strs) { |
26 |
| -Arrays.sort(strs,newComparator<String>() { |
27 |
| -@Override |
28 |
| -publicintcompare(Stringo1,Stringo2) { |
29 |
| -returno2.length() -o1.length(); |
30 |
| - } |
31 |
| - }); |
| 28 | +publicstaticclassSolution1 { |
| 29 | +/**Idea: if there's such a LUS there in the list, it must be one of the strings in the given list, |
| 30 | + so we'll just go through the list and check if one string is NOT subsequence of any others, if so, return it, otherwise, return -1*/ |
| 31 | +publicintfindLUSlength(String[]strs) { |
| 32 | +Arrays.sort(strs,newComparator<String>() { |
| 33 | +@Override |
| 34 | +publicintcompare(Stringo1,Stringo2) { |
| 35 | +returno2.length() -o1.length(); |
| 36 | + } |
| 37 | + }); |
32 | 38 |
|
33 |
| -for (inti =0;i <strs.length;i++) { |
34 |
| -booleanfound =true; |
35 |
| -for (intj =0;j <strs.length;j++) { |
36 |
| -if (i ==j) { |
37 |
| -continue; |
38 |
| - }elseif (isSubsequence(strs[i],strs[j])) { |
39 |
| -found =false; |
40 |
| -break; |
| 39 | +for (inti =0;i <strs.length;i++) { |
| 40 | +booleanfound =true; |
| 41 | +for (intj =0;j <strs.length;j++) { |
| 42 | +if (i ==j) { |
| 43 | +continue; |
| 44 | + }elseif (isSubsequence(strs[i],strs[j])) { |
| 45 | +found =false; |
| 46 | +break; |
| 47 | + } |
| 48 | + } |
| 49 | +if (found) { |
| 50 | +returnstrs[i].length(); |
41 | 51 | }
|
42 | 52 | }
|
43 |
| -if (found) { |
44 |
| -returnstrs[i].length(); |
45 |
| - } |
| 53 | +return -1; |
46 | 54 | }
|
47 |
| -return -1; |
48 |
| - } |
49 | 55 |
|
50 |
| -privatebooleanisSubsequence(Stringa,Stringb) { |
51 |
| -inti =0; |
52 |
| -for (intj =0;i <a.length() &&j <b.length();j++) { |
53 |
| -if (a.charAt(i) ==b.charAt(j)) { |
54 |
| -i++; |
| 56 | +privatebooleanisSubsequence(Stringa,Stringb) { |
| 57 | +inti =0; |
| 58 | +for (intj =0;i <a.length() &&j <b.length();j++) { |
| 59 | +if (a.charAt(i) ==b.charAt(j)) { |
| 60 | +i++; |
| 61 | + } |
55 | 62 | }
|
| 63 | +returni ==a.length(); |
56 | 64 | }
|
57 |
| -returni ==a.length(); |
58 | 65 | }
|
59 |
| - |
60 | 66 | }
|