|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * 521. Longest Uncommon Subsequence I |
| 5 | + * |
4 | 6 | * Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings.
|
5 | 7 | * 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.
|
6 | 8 | * A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements.
|
7 | 9 | * 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. |
9 | 12 |
|
10 | 13 | Example 1:
|
11 | 14 | Input: "aba", "cdc"
|
12 | 15 | Output: 3
|
13 | 16 | Explanation: The longest uncommon subsequence is "aba" (or "cdc"),
|
14 | 17 | because "aba" is a subsequence of "aba",
|
15 | 18 | but not a subsequence of any other strings in the group of two strings.
|
16 |
| - Note: |
17 | 19 |
|
| 20 | + Note: |
18 | 21 | Both strings' lengths will not exceed 100.
|
19 | 22 | Only letters from a ~ z will appear in input strings.
|
20 | 23 | */
|
21 | 24 | 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()); |
31 | 38 | }
|
32 |
| -returnMath.max(a.length(),b.length()); |
33 | 39 | }
|
34 |
| - |
35 | 40 | }
|