Skip to content

Commit a62c421

Browse files
committed
day 27
1 parent cbd0fb7 commit a62c421

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Longest Uncommon Subsequence II
3+
===============================
4+
5+
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.
6+
7+
An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.
8+
9+
A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.
10+
11+
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).
12+
13+
Example 1:
14+
Input: strs = ["aba","cdc","eae"]
15+
Output: 3
16+
17+
Example 2:
18+
Input: strs = ["aaa","aaa","aa"]
19+
Output: -1
20+
21+
Constraints:
22+
1 <= strs.length <= 50
23+
1 <= strs[i].length <= 10
24+
strs[i] consists of lowercase English letters.
25+
*/
26+
27+
class Solution {
28+
public:
29+
bool isSubseq(string& a, string& b) {
30+
int ia = 0, ib = 0;
31+
while(ia < a.size() && ib < b.size()) {
32+
if(a[ia] == b[ib]) {
33+
ia++;
34+
ib++;
35+
}
36+
else ib++;
37+
}
38+
39+
return ia == a.size();
40+
}
41+
42+
int findLUSlength(vector<string>& strs) {
43+
int ans = -1;
44+
45+
for(int i = 0; i < strs.size(); ++i) {
46+
int flag = 0;
47+
for(int j = 0; j < strs.size(); ++j) {
48+
if(i != j && isSubseq(strs[i], strs[j])) {
49+
flag = 1;
50+
break;
51+
}
52+
}
53+
54+
if(!flag) ans = max(ans, (int)strs[i].size());
55+
}
56+
57+
return ans;
58+
}
59+
};
60+

Leetcode Daily Challenge/August-2021/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
| 24. | [Complex Number Multiplication](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3917/) | [cpp](./24.%20Complex%20Number%20Multiplication.cpp) |
2929
| 25. | [Sum of Square Numbers](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3918/) | [cpp](./25.%20Sum%20of%20Square%20Numbers.cpp) |
3030
| 26. | [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3920/) | [cpp](./26.%20Verify%20Preorder%20Serialization%20of%20a%20Binary%20Tree.cpp) |
31-
| 27. | []() | [cpp](./27.%20.cpp) |
31+
| 27. | [Longest Uncommon Subsequence II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3921/) | [cpp](./27.%20Longest%20Uncommon%20Subsequence%20II.cpp) |
3232
| 28. | []() | [cpp](./28.%20.cpp) |

0 commit comments

Comments
 (0)