Longest Uncommon Subsequence II

medium array hash map two pointers string sorting

Problem

Find the length of the longest string that is a subsequence of one string in strs and not a subsequence of any other. Return −1 if none.

Inputstrs = ["aba","cdc","eae"]
Output3
Sort by len desc; for each, check it isn't a subsequence of any other.

def find_lus_length(strs):
    def is_sub(a, b):
        i = 0
        for c in b:
            if i < len(a) and a[i] == c: i += 1
        return i == len(a)
    strs.sort(key=lambda s: -len(s))
    for i, s in enumerate(strs):
        if all(not is_sub(s, strs[j]) for j in range(len(strs)) if j != i):
            return len(s)
    return -1
function findLUSlength(strs) {
  function isSub(a, b) {
    let i = 0;
    for (const c of b) if (i < a.length && a[i] === c) i++;
    return i === a.length;
  }
  strs.sort((a, b) => b.length - a.length);
  for (let i = 0; i < strs.length; i++) {
    let unique = true;
    for (let j = 0; j < strs.length; j++) {
      if (i !== j && isSub(strs[i], strs[j])) { unique = false; break; }
    }
    if (unique) return strs[i].length;
  }
  return -1;
}
class Solution {
    public int findLUSlength(String[] strs) {
        Arrays.sort(strs, (a, b) -> b.length() - a.length());
        for (int i = 0; i < strs.length; i++) {
            boolean unique = true;
            for (int j = 0; j < strs.length && unique; j++)
                if (i != j && isSub(strs[i], strs[j])) unique = false;
            if (unique) return strs[i].length();
        }
        return -1;
    }
    boolean isSub(String a, String b) {
        int i = 0;
        for (int k = 0; k < b.length() && i < a.length(); k++)
            if (a.charAt(i) == b.charAt(k)) i++;
        return i == a.length();
    }
}
bool isSub(string& a, string& b) {
    int i = 0;
    for (char c : b) if (i < (int)a.size() && a[i] == c) i++;
    return i == (int)a.size();
}
int findLUSlength(vector& strs) {
    sort(strs.begin(), strs.end(), [](auto& a, auto& b){ return a.size() > b.size(); });
    for (int i = 0; i < (int)strs.size(); i++) {
        bool unique = true;
        for (int j = 0; j < (int)strs.size() && unique; j++)
            if (i != j && isSub(strs[i], strs[j])) unique = false;
        if (unique) return strs[i].size();
    }
    return -1;
}
Time: O(n² · L) Space: O(1)