Given an array of strings strs, return the length of thelongest 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 "a _e_ b _d_ c" to get "abc". Other subsequences of "aebdc" include "aebdc", "aeb", and "" (empty string).
The longest uncommon subsequence must be one of the input strings. For each string, if it is not a subsequence of any other string, it is a candidate. We check all strings and return the length of the longest such string.
classSolution {
public:bool isSubseq(const string& a, const string& b) {
int i =0;
for (char c : b) if (i < a.size() && a[i] == c) ++i;
return i == a.size();
}
intfindLUSlength(vector<string>& strs) {
int ans =-1, n = strs.size();
for (int i =0; i < n; ++i) {
bool uncommon = true;
for (int j =0; j < n; ++j) {
if (i == j) continue;
if (isSubseq(strs[i], strs[j])) { uncommon = false; break; }
}
if (uncommon) ans = max(ans, (int)strs[i].size());
}
return ans;
}
};
classSolution {
booleanisSubseq(String a, String b) {
int i = 0;
for (char c : b.toCharArray()) if (i < a.length() && a.charAt(i) == c) i++;
return i == a.length();
}
publicintfindLUSlength(String[] strs) {
int ans =-1, n = strs.length;
for (int i = 0; i < n; ++i) {
boolean uncommon =true;
for (int j = 0; j < n; ++j) {
if (i == j) continue;
if (isSubseq(strs[i], strs[j])) { uncommon =false; break; }
}
if (uncommon) ans = Math.max(ans, strs[i].length());
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funisSubseq(a: String, b: String): Boolean {
var i = 0for (c in b) if (i < a.length && a[i] == c) i++return i == a.length
}
funfindLUSlength(strs: Array<String>): Int {
var ans = -1for (i in strs.indices) {
var uncommon = truefor (j in strs.indices) {
if (i == j) continueif (isSubseq(strs[i], strs[j])) { uncommon = false; break }
}
if (uncommon) ans = maxOf(ans, strs[i].length)
}
return ans
}
}
classSolution:
defisSubseq(self, a: str, b: str) -> bool:
i =0for c in b:
if i < len(a) and a[i] == c:
i +=1return i == len(a)
deffindLUSlength(self, strs: list[str]) -> int:
ans =-1 n = len(strs)
for i in range(n):
uncommon =Truefor j in range(n):
if i == j:
continueif self.isSubseq(strs[i], strs[j]):
uncommon =Falsebreakif uncommon:
ans = max(ans, len(strs[i]))
return ans
impl Solution {
pubfnfind_lus_length(strs: Vec<String>) -> i32 {
fnis_subseq(a: &str, b: &str) -> bool {
let (mut i, a_bytes) = (0, a.as_bytes());
for&c in b.as_bytes() {
if i < a_bytes.len() && a_bytes[i] == c { i +=1; }
}
i == a_bytes.len()
}
let n = strs.len();
letmut ans =-1;
for i in0..n {
letmut uncommon =true;
for j in0..n {
if i == j { continue; }
if is_subseq(&strs[i], &strs[j]) { uncommon =false; break; }
}
if uncommon {
ans = ans.max(strs[i].len() asi32);
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
isSubseq(a: string, b: string):boolean {
leti=0;
for (constcofb) if (i<a.length&&a[i] ===c) i++;
returni===a.length;
}
findLUSlength(strs: string[]):number {
letans=-1;
for (leti=0; i<strs.length; ++i) {
letuncommon=true;
for (letj=0; j<strs.length; ++j) {
if (i===j) continue;
if (this.isSubseq(strs[i], strs[j])) { uncommon=false; break; }
}
if (uncommon) ans= Math.max(ans, strs[i].length);
}
returnans;
}
}