comments | difficulty | edit_url | tags | |||||
---|---|---|---|---|---|---|---|---|
true |
Medium |
|
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
.
An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.
A subsequence of a string s
is a string that can be obtained after deleting any number of characters from s
.
- 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).
Example 1:
Input: strs = ["aba","cdc","eae"] Output: 3
Example 2:
Input: strs = ["aaa","aaa","aa"] Output: -1
Constraints:
2 <= strs.length <= 50
1 <= strs[i].length <= 10
strs[i]
consists of lowercase English letters.
We define a function
To determine if string
The time complexity is
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
def check(s: str, t: str):
i = j = 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
i += 1
j += 1
return i == len(s)
ans = -1
for i, s in enumerate(strs):
for j, t in enumerate(strs):
if i != j and check(s, t):
break
else:
ans = max(ans, len(s))
return ans
class Solution {
public int findLUSlength(String[] strs) {
int ans = -1;
int n = strs.length;
for (int i = 0, j; i < n; ++i) {
int x = strs[i].length();
for (j = 0; j < n; ++j) {
if (i != j && check(strs[i], strs[j])) {
x = -1;
break;
}
}
ans = Math.max(ans, x);
}
return ans;
}
private boolean check(String s, String t) {
int m = s.length(), n = t.length();
int i = 0;
for (int j = 0; i < m && j < n; ++j) {
if (s.charAt(i) == t.charAt(j)) {
++i;
}
}
return i == m;
}
}
class Solution {
public:
int findLUSlength(vector<string>& strs) {
int ans = -1;
int n = strs.size();
auto check = [&](const string& s, const string& t) {
int m = s.size(), n = t.size();
int i = 0;
for (int j = 0; i < m && j < n; ++j) {
if (s[i] == t[j]) {
++i;
}
}
return i == m;
};
for (int i = 0, j; i < n; ++i) {
int x = strs[i].size();
for (j = 0; j < n; ++j) {
if (i != j && check(strs[i], strs[j])) {
x = -1;
break;
}
}
ans = max(ans, x);
}
return ans;
}
};
func findLUSlength(strs []string) int {
ans := -1
check := func(s, t string) bool {
m, n := len(s), len(t)
i := 0
for j := 0; i < m && j < n; j++ {
if s[i] == t[j] {
i++
}
}
return i == m
}
for i, s := range strs {
x := len(s)
for j, t := range strs {
if i != j && check(s, t) {
x = -1
break
}
}
ans = max(ans, x)
}
return ans
}
function findLUSlength(strs: string[]): number {
const n = strs.length;
let ans = -1;
const check = (s: string, t: string): boolean => {
const [m, n] = [s.length, t.length];
let i = 0;
for (let j = 0; i < m && j < n; ++j) {
if (s[i] === t[j]) {
++i;
}
}
return i === m;
};
for (let i = 0; i < n; ++i) {
let x = strs[i].length;
for (let j = 0; j < n; ++j) {
if (i !== j && check(strs[i], strs[j])) {
x = -1;
break;
}
}
ans = Math.max(ans, x);
}
return ans;
}