522. Longest Uncommon Subsequence II
Input: "aba", "cdc", "eae"
Output: 3class Solution {
public int findLUSlength(String[] strs) {
int ans = -1;
for (int i = 0; i < strs.length; i++) {
boolean isSub = false;
for (int j = 0; j < strs.length; j++) {
if (i != j && isSubsequence(strs[i], strs[j])) {
isSub = true;
break;
}
}
if (!isSub) ans = Math.max(ans, strs[i].length());
}
return ans;
}
// check whether a is b's subsequence
public boolean isSubsequence(String a, String b) {
if (a.length() > b.length()) return false;
if (a.equals(b)) return true;
int index = 0;
for (int i = 0; i < b.length() && index < a.length(); i++) {
if (a.charAt(index) == b.charAt(i)) index++;
}
return index == a.length();
}
}Last updated