Skip to content

Commit 54b8ca6

Browse files
committed
+ problem 522
1 parent bd6b6b2 commit 54b8ca6

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 522. Longest Uncommon Subsequence II
2+
Given an array of strings `strs`, return *the length of the **longest uncommon subsequence** between them*. If the longest uncommon subsequence does not exist, return `-1`.
3+
4+
An **uncommon subsequence** between an array of strings is a string that is a **subsequence of one string but not the others**.
5+
6+
A **subsequence** of a string `s` is a string that can be obtained after deleting any number of characters from `s`.
7+
8+
* For example, `"abc"` is a subsequence of `"aebdc"` because you can delete the underlined characters in `"aebdc"` to get `"abc"`. Other subsequences of `"aebdc"` include `"aebdc"`, `"aeb"`, and `""` (empty string).
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> strs = ["aba","cdc","eae"]
13+
<strong>Output:</strong> 3
14+
</pre>
15+
16+
#### Example 2:
17+
<pre>
18+
<strong>Input:</strong> strs = ["aaa","aaa","aa"]
19+
<strong>Output:</strong> -1
20+
</pre>
21+
22+
#### Constraints:
23+
* `2 <= strs.length <= 50`
24+
* `1 <= strs[i].length <= 10`
25+
* `strs[i]` consists of lowercase English letters.
26+
27+
## Solutions (Rust)
28+
29+
### 1. Solution
30+
```Rust
31+
use std::collections::HashMap;
32+
use std::collections::HashSet;
33+
34+
impl Solution {
35+
pub fn find_lu_slength(strs: Vec<String>) -> i32 {
36+
let mut count = HashMap::new();
37+
38+
for s in &strs {
39+
let s = s.as_bytes();
40+
let mut subs = HashSet::new();
41+
42+
for x in 1..2_i32.pow(s.len() as u32) {
43+
let mut sub = vec![];
44+
45+
for i in 0..s.len() {
46+
if x & (1 << i) != 0 {
47+
sub.push(s[i]);
48+
}
49+
}
50+
51+
subs.insert(sub);
52+
}
53+
54+
for sub in subs.into_iter() {
55+
*count.entry(sub).or_insert(0) += 1;
56+
}
57+
}
58+
59+
count
60+
.iter()
61+
.filter(|&(_, c)| *c == 1)
62+
.map(|(sub, _)| sub.len() as i32)
63+
.max()
64+
.unwrap_or(-1)
65+
}
66+
}
67+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 522. 最长特殊序列 II
2+
给定字符串列表 `strs` ,返回其中 **最长的特殊序列** 的长度。如果最长特殊序列不存在,返回 `-1`
3+
4+
**特殊序列** 定义如下:该序列为某字符串 **独有的子序列(即不能是其他字符串的子序列)**
5+
6+
`s`**子序列**可以通过删去字符串 `s` 中的某些字符实现。
7+
8+
* 例如,`"abc"``"aebdc"` 的子序列,因为您可以删除`"aebdc"`中的下划线字符来得到 `"abc"``"aebdc"`的子序列还包括`"aebdc"``"aeb"``""` (空字符串)。
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> strs = ["aba","cdc","eae"]
13+
<strong>输出:</strong> 3
14+
</pre>
15+
16+
#### 示例 2:
17+
<pre>
18+
<strong>输入:</strong> strs = ["aaa","aaa","aa"]
19+
<strong>输出:</strong> -1
20+
</pre>
21+
22+
#### 提示:
23+
* `2 <= strs.length <= 50`
24+
* `1 <= strs[i].length <= 10`
25+
* `strs[i]` 只包含小写英文字母
26+
27+
## 题解 (Rust)
28+
29+
### 1. 题解
30+
```Rust
31+
use std::collections::HashMap;
32+
use std::collections::HashSet;
33+
34+
impl Solution {
35+
pub fn find_lu_slength(strs: Vec<String>) -> i32 {
36+
let mut count = HashMap::new();
37+
38+
for s in &strs {
39+
let s = s.as_bytes();
40+
let mut subs = HashSet::new();
41+
42+
for x in 1..2_i32.pow(s.len() as u32) {
43+
let mut sub = vec![];
44+
45+
for i in 0..s.len() {
46+
if x & (1 << i) != 0 {
47+
sub.push(s[i]);
48+
}
49+
}
50+
51+
subs.insert(sub);
52+
}
53+
54+
for sub in subs.into_iter() {
55+
*count.entry(sub).or_insert(0) += 1;
56+
}
57+
}
58+
59+
count
60+
.iter()
61+
.filter(|&(_, c)| *c == 1)
62+
.map(|(sub, _)| sub.len() as i32)
63+
.max()
64+
.unwrap_or(-1)
65+
}
66+
}
67+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::collections::HashMap;
2+
use std::collections::HashSet;
3+
4+
impl Solution {
5+
pub fn find_lu_slength(strs: Vec<String>) -> i32 {
6+
let mut count = HashMap::new();
7+
8+
for s in &strs {
9+
let s = s.as_bytes();
10+
let mut subs = HashSet::new();
11+
12+
for x in 1..2_i32.pow(s.len() as u32) {
13+
let mut sub = vec![];
14+
15+
for i in 0..s.len() {
16+
if x & (1 << i) != 0 {
17+
sub.push(s[i]);
18+
}
19+
}
20+
21+
subs.insert(sub);
22+
}
23+
24+
for sub in subs.into_iter() {
25+
*count.entry(sub).or_insert(0) += 1;
26+
}
27+
}
28+
29+
count
30+
.iter()
31+
.filter(|&(_, c)| *c == 1)
32+
.map(|(sub, _)| sub.len() as i32)
33+
.max()
34+
.unwrap_or(-1)
35+
}
36+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@
341341
[518][518l] |[Coin Change 2][518] |![rs]
342342
[520][520l] |[Detect Capital][520] |![rs]
343343
[521][521l] |[Longest Uncommon Subsequence I][521] |![rb]&nbsp;&nbsp;![rs]
344+
[522][522l] |[Longest Uncommon Subsequence II][522] |![rs]
344345
[524][524l] |[Longest Word in Dictionary through Deleting][524] |![py]
345346
[525][525l] |[Contiguous Array][525] |![rb]&nbsp;&nbsp;![rs]
346347
[526][526l] |[Beautiful Arrangement][526] |![rs]
@@ -1835,6 +1836,7 @@
18351836
[518]:Problemset/0518-Coin%20Change%202/README.md#518-coin-change-2
18361837
[520]:Problemset/0520-Detect%20Capital/README.md#520-detect-capital
18371838
[521]:Problemset/0521-Longest%20Uncommon%20Subsequence%20I/README.md#521-longest-uncommon-subsequence-i
1839+
[522]:Problemset/0522-Longest%20Uncommon%20Subsequence%20II/README.md#522-longest-uncommon-subsequence-ii
18381840
[524]:Problemset/0524-Longest%20Word%20in%20Dictionary%20through%20Deleting/README.md#524-longest-word-in-dictionary-through-deleting
18391841
[525]:Problemset/0525-Contiguous%20Array/README.md#525-contiguous-array
18401842
[526]:Problemset/0526-Beautiful%20Arrangement/README.md#526-beautiful-arrangement
@@ -3327,6 +3329,7 @@
33273329
[518l]:https://leetcode.com/problems/coin-change-2/
33283330
[520l]:https://leetcode.com/problems/detect-capital/
33293331
[521l]:https://leetcode.com/problems/longest-uncommon-subsequence-i/
3332+
[522l]:https://leetcode.com/problems/longest-uncommon-subsequence-ii/
33303333
[524l]:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/
33313334
[525l]:https://leetcode.com/problems/contiguous-array/
33323335
[526l]:https://leetcode.com/problems/beautiful-arrangement/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@
341341
[518][518l] |[零钱兑换 II][518] |![rs]
342342
[520][520l] |[检测大写字母][520] |![rs]
343343
[521][521l] |[最长特殊序列 Ⅰ][521] |![rb]&nbsp;&nbsp;![rs]
344+
[522][522l] |[最长特殊序列 II][522] |![rs]
344345
[524][524l] |[通过删除字母匹配到字典里最长单词][524] |![py]
345346
[525][525l] |[连续数组][525] |![rb]&nbsp;&nbsp;![rs]
346347
[526][526l] |[优美的排列][526] |![rs]
@@ -1835,6 +1836,7 @@
18351836
[518]:Problemset/0518-Coin%20Change%202/README_CN.md#518-零钱兑换-ii
18361837
[520]:Problemset/0520-Detect%20Capital/README_CN.md#520-检测大写字母
18371838
[521]:Problemset/0521-Longest%20Uncommon%20Subsequence%20I/README_CN.md#521-最长特殊序列-i
1839+
[522]:Problemset/0522-Longest%20Uncommon%20Subsequence%20II/README_CN.md#522-最长特殊序列-ii
18381840
[524]:Problemset/0524-Longest%20Word%20in%20Dictionary%20through%20Deleting/README_CN.md#524-通过删除字母匹配到字典里最长单词
18391841
[525]:Problemset/0525-Contiguous%20Array/README_CN.md#525-连续数组
18401842
[526]:Problemset/0526-Beautiful%20Arrangement/README_CN.md#526-优美的排列
@@ -3327,6 +3329,7 @@
33273329
[518l]:https://leetcode.cn/problems/coin-change-2/
33283330
[520l]:https://leetcode.cn/problems/detect-capital/
33293331
[521l]:https://leetcode.cn/problems/longest-uncommon-subsequence-i/
3332+
[522l]:https://leetcode.cn/problems/longest-uncommon-subsequence-ii/
33303333
[524l]:https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting/
33313334
[525l]:https://leetcode.cn/problems/contiguous-array/
33323335
[526l]:https://leetcode.cn/problems/beautiful-arrangement/

0 commit comments

Comments
 (0)