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

Commit56ed7da

Browse files
refactor 521
1 parent2ecd954 commit56ed7da

File tree

1 file changed

+18
-13
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+18
-13
lines changed
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
packagecom.fishercoder.solutions;
22

33
/**
4+
* 521. Longest Uncommon Subsequence I
5+
*
46
* Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings.
57
* 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.
68
* A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements.
79
* Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
8-
* The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
10+
* The input will be two strings, and the output needs to be the length of the longest uncommon subsequence.
11+
* If the longest uncommon subsequence doesn't exist, return -1.
912
1013
Example 1:
1114
Input: "aba", "cdc"
1215
Output: 3
1316
Explanation: The longest uncommon subsequence is "aba" (or "cdc"),
1417
because "aba" is a subsequence of "aba",
1518
but not a subsequence of any other strings in the group of two strings.
16-
Note:
1719
20+
Note:
1821
Both strings' lengths will not exceed 100.
1922
Only letters from a ~ z will appear in input strings.
2023
*/
2124
publicclass_521 {
22-
//The gotcha point of this question is:
23-
//1. if a and b are identical, then there will be no common subsequence, return -1
24-
//2. else if a and b are of equal length, then any one of them will be a subsequence of the other string
25-
//3. else if a and b are of different length, then the longer one is a required subsequence because the longer string cannot be a subsequence of the shorter one
26-
27-
//Or in other words, when a.length() != b.length(), no subsequence of b will be equal to a, so return Math.max(a.length(), b.length())
28-
publicintfindLUSlength(Stringa,Stringb) {
29-
if (a.equals(b)) {
30-
return -1;
25+
publicstaticclassSolution1 {
26+
/**
27+
* The gotcha point of this question is:
28+
* 1. if a and b are identical, then there will be no common subsequence, return -1
29+
* 2. else if a and b are of equal length, then any one of them will be a subsequence of the other string
30+
* 3. else if a and b are of different length, then the longer one is a required subsequence because the longer string cannot be a subsequence of the shorter one
31+
* Or in other words, when a.length() != b.length(), no subsequence of b will be equal to a, so return Math.max(a.length(), b.length())
32+
*/
33+
publicintfindLUSlength(Stringa,Stringb) {
34+
if (a.equals(b)) {
35+
return -1;
36+
}
37+
returnMath.max(a.length(),b.length());
3138
}
32-
returnMath.max(a.length(),b.length());
3339
}
34-
3540
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp