Skip to content

Commit 0e26c96

Browse files
committed
feat: add solutions to lc problems: No.0521,0522
* No.0521.Longest Uncommon Subsequence * No.0522.Longest Uncommon Subsequence II
1 parent eeaa620 commit 0e26c96

File tree

12 files changed

+452
-117
lines changed

12 files changed

+452
-117
lines changed

solution/0500-0599/0521.Longest Uncommon Subsequence I/README.md

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,49 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给你两个字符串,请你从这两个字符串中找出最长的特殊序列。</p>
9+
<p>给你两个字符串&nbsp;<code>a</code>&nbsp;&nbsp;<code>b</code>,请返回 <em>这两个字符串中 <strong>最长的特殊序列</strong>&nbsp;</em> 。如果不存在,则返回 <code>-1</code>&nbsp;。</p>
1010

11-
<p>「最长特殊序列」定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列)。</p>
11+
<p><strong>「最长特殊序列」</strong>&nbsp;定义如下:该序列为&nbsp;<strong>某字符串独有的最长子序列(即不能是其他字符串的子序列)</strong>&nbsp;。</p>
1212

13-
<p><strong>子序列</strong> 可以通过删去字符串中的某些字符实现,但不能改变剩余字符的相对顺序。空序列为所有字符串的子序列,任何字符串为其自身的子序列。</p>
13+
<p>字符串&nbsp;<code>s</code>&nbsp;的子序列是在从&nbsp;<code>s</code>&nbsp;中删除任意数量的字符后可以获得的字符串。</p>
1414

15-
<p>输入为两个字符串,输出最长特殊序列的长度。如果不存在,则返回 -1。</p>
15+
<ul>
16+
<li>例如,<code>“abc”</code>&nbsp;是 <code>“aebdc”</code> 的子序列,因为您可以删除 <code>“aebdc”</code> 中的下划线字符来得到 <code>“abc”</code> 。 <code>“aebdc”</code> 的子序列还包括 <code>“aebdc”</code> 、 <code>“aeb”</code> 和 <code>“”</code> (空字符串)。</li>
17+
</ul>
1618

1719
<p>&nbsp;</p>
1820

1921
<p><strong>示例 1:</strong></p>
2022

21-
<pre><strong>输入:</strong> &quot;aba&quot;, &quot;cdc&quot;
23+
<pre>
24+
<strong>输入:</strong> a = "aba", b = "cdc"
2225
<strong>输出:</strong> 3
23-
<strong>解释:</strong> 最长特殊序列可为 &quot;aba&quot; (或 &quot;cdc&quot;),两者均为自身的子序列且不是对方的子序列。</pre>
26+
<strong>解释:</strong> 最长特殊序列可为 "aba" (或 "cdc"),两者均为自身的子序列且不是对方的子序列。</pre>
2427

2528
<p><strong>示例 2:</strong></p>
2629

27-
<pre><strong>输入:</strong>a = &quot;aaa&quot;, b = &quot;bbb&quot;
30+
<pre>
31+
<strong>输入:</strong>a = "aaa", b = "bbb"
2832
<strong>输出:</strong>3
33+
<strong>解释:</strong> 最长特殊序列是“aaa”和“bbb”。
2934
</pre>
3035

3136
<p><strong>示例 3:</strong></p>
3237

33-
<pre><strong>输入:</strong>a = &quot;aaa&quot;, b = &quot;aaa&quot;
38+
<pre>
39+
<strong>输入:</strong>a = "aaa", b = "aaa"
3440
<strong>输出:</strong>-1
41+
<strong>解释:</strong> 字符串a的每个子序列也是字符串b的每个子序列。同样,字符串b的每个子序列也是字符串a的子序列。
3542
</pre>
3643

3744
<p>&nbsp;</p>
3845

3946
<p><strong>提示:</strong></p>
4047

41-
<ol>
42-
<li>两个字符串长度均处于区间&nbsp;<code>[1 - 100]</code></li>
43-
<li>字符串中的字符仅含有&nbsp;<code>&#39;a&#39;~&#39;z&#39;</code></li>
44-
</ol>
48+
<ul>
49+
<li><code>1 &lt;= a.length, b.length &lt;= 100</code></li>
50+
<li><code>a</code>&nbsp;和&nbsp;<code>b</code>&nbsp;由小写英文字母组成</li>
51+
</ul>
4552

4653
## 解法
4754

@@ -55,7 +62,7 @@
5562

5663
而特殊序列则是求**非子序列**,此时列举 `a` 的子序列 `"abc"``b` 拿不出来,那这就是一个成功的非子序列。
5764

58-
如此,在 `a != b` 时,谁最长谁就是 *最长的特殊序列*
65+
如此,在 `a != b` 时,谁最长谁就是 _最长的特殊序列_
5966

6067
<!-- tabs:start -->
6168

@@ -65,12 +72,7 @@
6572

6673
```python
6774
class Solution:
68-
def findLUSlength(self, a, b):
69-
"""
70-
:type a: str
71-
:type b: str
72-
:rtype: int
73-
"""
75+
def findLUSlength(self, a: str, b: str) -> int:
7476
return -1 if a == b else max(len(a), len(b))
7577
```
7678

@@ -81,9 +83,7 @@ class Solution:
8183
```java
8284
class Solution {
8385
public int findLUSlength(String a, String b) {
84-
if (a.equals(b))
85-
return -1;
86-
return Math.max(a.length(), b.length());
86+
return a.equals(b) ? -1 : Math.max(a.length(), b.length());
8787
}
8888
}
8989
```
@@ -101,6 +101,31 @@ impl Solution {
101101
}
102102
```
103103

104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int findLUSlength(string a, string b) {
110+
return a == b ? -1 : max(a.size(), b.size());
111+
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
func findLUSlength(a string, b string) int {
119+
if a == b {
120+
return -1
121+
}
122+
if len(a) > len(b) {
123+
return len(a)
124+
}
125+
return len(b)
126+
}
127+
```
128+
104129
### **...**
105130

106131
```

solution/0500-0599/0521.Longest Uncommon Subsequence I/README_EN.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
## Description
66

7-
<p>Given two strings <code>a</code>&nbsp;and <code>b</code>, find the length of the&nbsp;<strong>longest uncommon subsequence</strong>&nbsp;between them.</p>
7+
<p>Given two strings <code>a</code> and <code>b</code>, return <em>the length of the <strong>longest uncommon subsequence</strong> between </em><code>a</code> <em>and</em> <code>b</code>. If the longest uncommon subsequence does not exist, return <code>-1</code>.</p>
88

9-
<p>A&nbsp;<b>subsequence</b>&nbsp;of&nbsp;a string&nbsp;<code>s</code>&nbsp;is a string that can be obtained after deleting any number of characters from <code>s</code>. For example, <code>&quot;abc&quot;</code>&nbsp;is a subsequence of <code>&quot;aebdc&quot;</code>&nbsp;because you can delete the underlined characters in&nbsp;<code>&quot;a<u>e</u>b<u>d</u>c&quot;</code>&nbsp;to get <code>&quot;abc&quot;</code>. Other subsequences of&nbsp;<code>&quot;aebdc&quot;</code>&nbsp;include&nbsp;<code>&quot;aebdc&quot;</code>,&nbsp;<code>&quot;aeb&quot;</code>,&nbsp;and&nbsp;<code>&quot;&quot;</code>&nbsp;(empty string).</p>
9+
<p>An <strong>uncommon subsequence</strong> between two strings is a string that is a <strong>subsequence of one but not the other</strong>.</p>
1010

11-
<p>An&nbsp;<strong>uncommon subsequence</strong>&nbsp;between two strings&nbsp;is a string that is a <strong>subsequence of one&nbsp;but not the other</strong>.</p>
11+
<p>A <strong>subsequence</strong> of a string <code>s</code> is a string that can be obtained after deleting any number of characters from <code>s</code>.</p>
1212

13-
<p>Return <em>the length of the <strong>longest uncommon subsequence</strong>&nbsp;between <code>a</code>&nbsp;and <code>b</code></em>. If the longest uncommon subsequence doesn&#39;t exist, return <code>-1</code>.</p>
13+
<ul>
14+
<li>For example, <code>&quot;abc&quot;</code> is a subsequence of <code>&quot;aebdc&quot;</code> because you can delete the underlined characters in <code>&quot;a<u>e</u>b<u>d</u>c&quot;</code> to get <code>&quot;abc&quot;</code>. Other subsequences of <code>&quot;aebdc&quot;</code> include <code>&quot;aebdc&quot;</code>, <code>&quot;aeb&quot;</code>, and <code>&quot;&quot;</code> (empty string).</li>
15+
</ul>
1416

1517
<p>&nbsp;</p>
1618
<p><strong>Example 1:</strong></p>
@@ -54,12 +56,7 @@ Note that &quot;cdc&quot; is also a longest uncommon subsequence.
5456

5557
```python
5658
class Solution:
57-
def findLUSlength(self, a, b):
58-
"""
59-
:type a: str
60-
:type b: str
61-
:rtype: int
62-
"""
59+
def findLUSlength(self, a: str, b: str) -> int:
6360
return -1 if a == b else max(len(a), len(b))
6461
```
6562

@@ -68,9 +65,7 @@ class Solution:
6865
```java
6966
class Solution {
7067
public int findLUSlength(String a, String b) {
71-
if (a.equals(b))
72-
return -1;
73-
return Math.max(a.length(), b.length());
68+
return a.equals(b) ? -1 : Math.max(a.length(), b.length());
7469
}
7570
}
7671
```
@@ -88,6 +83,31 @@ impl Solution {
8883
}
8984
```
9085

86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int findLUSlength(string a, string b) {
92+
return a == b ? -1 : max(a.size(), b.size());
93+
}
94+
};
95+
```
96+
97+
### **Go**
98+
99+
```go
100+
func findLUSlength(a string, b string) int {
101+
if a == b {
102+
return -1
103+
}
104+
if len(a) > len(b) {
105+
return len(a)
106+
}
107+
return len(b)
108+
}
109+
```
110+
91111
### **...**
92112

93113
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int findLUSlength(string a, string b) {
4+
return a == b ? -1 : max(a.size(), b.size());
5+
}
6+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func findLUSlength(a string, b string) int {
2+
if a == b {
3+
return -1
4+
}
5+
if len(a) > len(b) {
6+
return len(a)
7+
}
8+
return len(b)
9+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class Solution {
22
public int findLUSlength(String a, String b) {
3-
if (a.equals(b))
4-
return -1;
5-
return Math.max(a.length(), b.length());
3+
return a.equals(b) ? -1 : Math.max(a.length(), b.length());
64
}
7-
}
5+
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
class Solution:
2-
def findLUSlength(self, a, b):
3-
"""
4-
:type a: str
5-
:type b: str
6-
:rtype: int
7-
"""
2+
def findLUSlength(self, a: str, b: str) -> int:
83
return -1 if a == b else max(len(a), len(b))

0 commit comments

Comments
 (0)