Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf6526b1

Browse files
refactor 522
1 parent11cd4e9 commitf6526b1

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
importjava.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
publicclass_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-
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+
});
3238

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();
4151
}
4252
}
43-
if (found) {
44-
returnstrs[i].length();
45-
}
53+
return -1;
4654
}
47-
return -1;
48-
}
4955

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+
}
5562
}
63+
returni ==a.length();
5664
}
57-
returni ==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
publicclass_522Test {
1313

14-
privatestatic_522test;
14+
privatestatic_522.Solution1solution1;
1515
privatestaticintexpected;
1616
privatestaticintactual;
1717
privatestaticString[]strs;
1818

1919
@BeforeClass
2020
publicstaticvoidsetup() {
21-
test =new_522();
21+
solution1 =new_522.Solution1();
2222
}
2323

2424
@Test
2525
publicvoidtest1() {
2626
strs =newString[]{"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)

[8]ページ先頭

©2009-2025 Movatter.jp