8000 refactor 522 · devdcores/Leetcode@f6526b1 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit f6526b1

Browse files
refactor 522
1 parent 11cd4e9 commit f6526b1

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

src/main/java/com/fishercoder/solutions/_522.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
import java.util.Comparator;
55

66
/**
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.
1217
1318
Example 1:
1419
Input: "aba", "cdc", "eae"
@@ -20,41 +25,42 @@
2025
*/
2126
public class _522 {
2227

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-
public int findLUSlength(String[] strs) {
26-
Arrays.sort(strs, new Comparator<String>() {
27-
@Override
28-
public int compare(String o1, String o2) {
29-
return o2.length() - o1.length();
30-
}
31-
});
28+
public static class Solution1 {
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+
public int findLUSlength(String[] strs) {
32+
Arrays.sort(strs, new Comparator<String>() {
33+
@Override
34+
public int compare(String o1, String o2) {
35+
return o2.length() - o1.length();
36+
}
37+
});
3238

33-
for (int i = 0; i < strs.length; i++) {
34-
boolean found = true;
35-
for (int j = 0; j < strs.length; j++) {
36-
if (i == j) {
37-
continue;
38-
} else if (isSubsequence(strs[i], strs[j])) {
39-
found = false;
40-
break;
39+
for (int i = 0; i < strs.length; i++) {
40+
boolean found = true;
41+
for (int j = 0; j < strs.length; j++) {
42+
if (i == j) {
43+
continue;
44+
} else if (isSubsequence(strs[i], strs[j])) {
45+
found = false;
46+
break;
47+
}
48+
}
49+
if (found) {
50+
return strs[i].length();
4151
}
4252
}
43-
if (found) {
44-
return strs[i].length();
45-
}
53+
return -1;
4654
}
47-
return -1;
48-
}
4955

50-
private boolean isSubsequence(String a, String b) {
51-
int i = 0;
52-
for (int j = 0; i < a.length() && j < b.length(); j++) {
53-
if (a.charAt(i) == b.charAt(j)) {
54-
i++;
56+
private boolean isSubsequence(String a, String b) {
57+
int i = 0;
58+
for (int j = 0; i < a.length() && j < b.length(); j++) {
59+
if (a.charAt(i) == b.charAt(j)) {
60+
i++;
61+
}
5562
}
63+
return i == a.length();
5664
}
57-
return i == a.length();
5865
}
59-
6066
}

src/test/java/com/fishercoder/_522Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
*/
1212
public class _522Test {
1313

14-
private static _522 test;
14+
private static _522.Solution1 solution1;
1515
private static int expected;
1616
private static int actual;
1717
private static String[] strs;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
test = new _522();
21+
solution1 = new _522.Solution1();
2222
}
2323

2424
@Test
2525
public void test1() {
2626
strs = new String[]{"aaa", "aaa", "aa"};
2727
expected = -1;
28-
actual = test.findLUSlength(strs);
28+
actual = solution1.findLUSlength(strs);
2929
assertEquals(expected, actual);
3030
}
3131

0 commit comments

Comments
 (0)
0